Вы находитесь на странице: 1из 10

Soft Computing Lab, RCH-554

Q1. Search of bracketed root .

Algorithm for Search Interval Using the Tabulation Method: This algorithm is
transformed to a computer program written in C++ language . Let xi and xf be the
initial and final values of the interval over which y = f (x) is to be tabulated. Let Dx
be the increment in the value of x.

1. Define the inline function f (x) = x3 – 4x – 9.


2. Declare/define the function/subprogram sit to find the search interval.
3. Start the (main) function.
4. Define the main function which calls the appropriate subprogram/ function
according to the given choice.
5. Stop (main)

Details of function/subprogram sit:


1. Start
2. 2. Input the initial value xi , the final value xf and the increment Dx.
3. Let x = xi
4. While x<xf y = f(x)
5. output x, y
6. x = x + Dx
7. Endwhile
8. Return

//Search interval by tabulation method (Code)


#include< iostream.h>
#include <conio.h>

inline float func(float x)


{ return (x*x*x-4*x-9); }
void sit()
{
float xi,xf,dx,x,y;
char option;
cout<<"For default input values enter 'y' else 'n'\n";
cin>>option;
if (option=='y')
{ xi=1; xf=4; dx=1; }
else { cout<<"input initial,final,step-size values, xi xf dx\n"; cin>>xi>>xf>>dx; }
x=xi;
cout<<"x y\n";
while (x<xf)

Q2. Algorithm for Fixed-Point Iteration Method :

This algorithm is transformed to a computer program written in C++ language. Let a


be the initial value of the root.
1. Define the inline function f(x) = x3 – 4x – 9. \
2. Define eps = 0.0001.
3. Declare/define the function/subprogram fpt to find the search interval. 4. Start the
(main) function.
5. Define the main function which calls the subprogram/function according to given
choice.
6. Stop (main).
Details of function/subprogram fpt:
1. Start
2. Input the initial value of root a
3. Input maximum number of iterations, maxit
4. For count = 1 to maxit in steps of 1
c = f(a) if |(c – a)/c| > eps a = c
else
exit for loop endfor
5. Output root, count, f(root)
6. Return

//FIXED-point iteration method (Code)


#include<iostream.h>
#include<conio.h>
#define eps 0.0001
inline float f(float x)
{ return 1/(9-x*x); }
void fpt()
{ float a,c;
int count,maxit;
char option;
cout<<"For default input values enter 'y' else 'n'\n";
cin>>option; if (option=='y') a=0;
else
{ cout<<"input initial root value\n";
cin>>a; }
maxit=50;
for (count=1;count<=maxit;count++)
{c=f(a); if (fabs((c-a)/c)>eps)
a=c;
Else
break;
}
cout<<"root= "<<c<<endl;
cout<<"f(root)= "<<f(c)<<endl;
cout<<"iterations= "<<count<<endl; }
main()
{ fpt(); }

Q3. Solution of a single non-linear equation by Newton Raphson method.

Algorithm for Newton–Raphson Method: This algorithm is transformed to a


computer program written in C++ language . Let a be the initial value of the root for
which y = f (x) is to be tabulated for finding the root value.
1. Define the inline function f (x) = x2 + x – 2.
2. Define the inline function derf = 2x + 1 for derivative of f (x).
3. Define eps = 0.0001.
4. Declare/define the function/subprogram ntrp to find the root.
5. Start the (main) function.
6. Define the main function which calls the subprogram/function according to the
given choice.
7. Stop (main).

Details of function/subprogram ntrp:


1. Start
2. Input the initial root value, a
3. count = 0
do
if derf(a) > 0
h1 = f(a)/(derf(a))
c = a–h1
count = count +1
if (|((c–a)/c)| < eps and f(c) = 0)

//Newton-Raphson method (Code)

#include <iostream.h>
#include<conio.h>
#define eps 0.00001
inline float f(float x)
{ return x*x*x-9*x+1; }
inline float derf(float x)
{ return (3*x*x-9); }
void ntrp() { float a,c,h1;
int count; char option;
cout<<"For default input values enter 'y' else 'n'\n";
cin>>option;
if (option=='y')
{ a=2; }
else
{
cout<<"input initial root value a\n";

cin>>a; }
count=0;
do
{ if (derf(a)>0)
h1=f(a)/derf(a);
c=a-h1; count=count+1;
if ((fabs((c-a)/c)<eps) && (f(c)==0))
break;
else a=c; }
while ((count<=20) && fabs(f(c))>eps);
cout<<"root= "<<c<<endl; cout<<"f(c)= "<<f(c)<<endl;
cout<<"iterations= "<<count<<endl;
}
main()
{ ntrp();
}

Q4. Algorithm for False Position Method:

This algorithm is transformed to a computer program written in C++ language. Let a


and b be the initial and final values of the interval over which y = f (x) is to be
tabulated for finding the root value.
1. Define the inline function f (x) = x3 – 4x – 9.
2. Define eps = 0.0001.
3. Declare/define the function/subprogram regfal to find the root.
4. Start the (main) function.
5. Define the main function which calls the subprogram/function according to given
choice.
6. Stop (main).
Details of function/subprogram regfal:
1. Start
2. Input the initial root interval a, b.
3. 3. If f(a)*f(b)< 0
b=c
else
a=c
endif
count = count + 1
while ((count <= 20) or (f(c) > eps)
output ‘root =’, c output count, f(c) else output ‘initial interval incorrect’
4. Return

//Regula-Falsi/false position method


#include<iostream.h>
#include<conio.h>
#define eps 0.0001
inline float f(float x) { return x*x*x-9*x+1; }
void regfal()
{
float a,b,c;
int count=0;
char option;
cout<<"For default input values enter 'y' else 'n'\n";
cin>>option;
if (option=='y') { a=2; b=3; }
else
{ cout<<"input initial root interval a, b\n";
cin>>a>>b; }
if ((f(a)*f(b))<0)
{ count=0;
do
{ c=(a*f(b)-b*f(a))/(f(b)-f(a))
; if (f(a)*f(c)<0) b=c;
else a=c;
count=count+1; }
while ((count<=20) && fabs(f(c))>eps);
cout<<"root= "<<c<<endl;
cout<<"f(root)= "<<f(c)<<endl;
cout<<"iterations= "<<count<<endl;
}
}
main()
{
regfal();
}
Computational mathematics The term computational mathematics is unlikely
to inspire much enthusiasm with you as a second year process engineering
students. You might think that both elements - computing and mathematics
- are relatively irrelevant, abstract, and difficult for the process
engineer. Yet, within the next year or so you will realise that both are
essential tools for you to understand advanced engineering subjects and
later as a professional engineer you will be using as an integral part
of problem solving. The reason why they may seem so irrelevant, abstract
and difficult is that they are typically taught out of context as separate
entities. You learn about them as separate entities removed from the
engineering problems they are supposed to help you solve. The computer
is for the engineer what the toolbox is for the tradesperson - a collection
of tools that enables him or her to do his or her job. Imagine spending
two years as an apprentice learning about the hammer without ever been
shown the nail and the pieces of wood it is going to be used to join. The
reason for this odd way of teaching math and computing is partly historical
and partly due to lack of good tools. An integrated approach to solving
process engineering problems using computational mathematics involves 1.
Formulating the physical problem, for example by using a mass balance.
2. Formulating the problem as a mathematical problem. 3. Choosing an
appropriate numerical method to solve the problem. 4. Implementing the
numerical method on the computer. 5. Presenting the results in a suitable
form. Steps 2, 3 and 4 each represents a level of abstraction away from
the physical problem. Until recently, the final level of abstraction was
too large to be fully understood for most first and second year students.
The development of powerful computational and symbolic math packages such
as MATLAB, Maple and Mathematica has partly rectified this situation.
These packages almost totally remove the abstraction between the
numerical method formulation and the computational solution. They also
go some way to reduce the abstraction between the analytical mathematical
formulation and the numerical formulation. This text will teach you how
to use MATLAB as a tool to solve process engineering problems. The text
is divided into three parts. The first part of the book will teach you
about the various building blocks used to write MATLAB programs (Chapter
2 to 6) and how efficiently to write programs (Chapter 7). The second part
is divided into chapters based on the type of mathematical problem
considered. Each chapter describes process engineering examples
resulting in the type of mathematical problem considered, followed by a

Вам также может понравиться