CONM Submission

Preview:

Citation preview

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 1

Statement: Write down the Matlab Program using Newton-Raphson method for any equation.

Solution:

Input :

clc; clear all; % Clears the workspace format long;

% Take i/p from user a=input('\n Enter the function : ','s'); b=input('\n Enter the derivative of function : ','s');

% Converts the input string into symbolic function ft=inline(a); dft=inline(b); n=input('enter no. of significant digits: '); t0=0; epsilon_s=(0.5*10^(2-n)); epsilon_a=100; tr=fzero((ft),t0); % Solver disp (tr); varun=sprintf('BY NEWTON-RAPHSON METHOD:'); disp(varun); tx=input('Enter your initial guess for root: '); td=tx; head=sprintf('Time \t\t\t\t\tepsilon_a \t\t\t\t\tepsilon_t

\t\t\t\t\tepsilon_s '); disp(head); while (epsilon_a>=epsilon_s) tnew=td-(ft(td)/dft(td)); epsilon_a=abs((tnew-td)/tnew)*100; epsilon_t=abs((tr-tnew)/tr)*100; td=tnew; table=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t %f \t\t\t

%f',tnew,epsilon_a,epsilon_t,epsilon_s); disp(table); end

% Prints the answer fprintf('\n \n The root of the equation is : %f \n',tnew)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

Enter the function : (exp(t))*cos(t)-1.4

Enter the derivative of function : (exp(t))*cos(t)-(exp(t))*sin(t)

enter no. of significant digits: 4

0.433560875352657

BY NEWTON-RAPHSON METHOD:

Enter your initial guess for root: 0

Time epsilon_a epsilon_t epsilon_s

4.000000e-001 100.000000 7.740752743 0.005000

4.327044e-001 7.558146 0.197537266 0.005000

4.335602e-001 0.197392 0.000145353 0.005000

4.335609e-001 0.000145 0.000000000 0.005000

The root of the equation is : 0.433561

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 2

Statement: Write down the Matlab Program using Modified Newton-Raphson method for any

equation.

Solution:

Input :

clc; Clear all; % Clears the workspace

% Takes the i/p from user a=input('\n Enter the function : ','s'); b=input('\n Enter the derivative of function : ','s'); c=input('\n Enter second order derivative : ','s'); x0=0;

% Converts the input string into symbolic function fx=inline(a); dfx=inline(b); d2fx=inline(c); n=input('Enter number of significant digits: '); epsilon_s=(0.5*10^(2-n)); tr=fzero((fx),0); % Using solver disp (tr); v=input('\n Enter your initial guess for root : '); told=v; varun=sprintf('BY MODIFIED NEWTON-RAPHSON METHOD:'); disp(varun); head=sprintf('Time \t\t\t\t\tepsilon_a \t\t\t\t\tepsilon_t

\t\t\t\t\tepsilon_s '); disp(head); while(1) tnew=told-((fx(told)*dfx(told)/((dfx(told)^2)-(fx(told)*d2fx(told))))); err=abs((tnew-told)/tnew)*100; epsilon_t=abs((tr-tnew)/tr)*100; told=tnew; table=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t %f \t\t\t

%f',tnew,err,epsilon_t,epsilon_s); disp(table); if (err<=epsilon_s) break; end end fprintf('\n \n The root of the equation is : %f \n',tnew)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output

Enter the function : x*sin(x)+cos(x)

Enter the derivative of function : x*cos(x)

Enter second order derivative : cos(x)-(x*sin(x))

Enter number of significant digits: 5

-2.7984

Enter your initial guess for root : 6

BY MODIFIED NEWTON-RAPHSON METHOD:

Time epsilon_a epsilon_t epsilon_s

6.117645e+000 1.923040 318.613323399 0.000500

6.121248e+000 0.058870 318.742096720 0.000500

6.121250e+000 0.000035 318.742173765 0.000500

The root of the equation is : 6.121250

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 3

Statement: Write down the Matlab Program using successive approximation method for any

equation.

Solution:

Input :

clc; clear; g=input('Enter the function:','s'); f=inline(g); % Defining function n=input('Enter number of significant digits: '); es=(0.5*10^(2-n)); % Stopping criteria ea=100; t0=0; t=input('Enter initial guess: '); tr=fzero((f),t0); % Calculating true roots disp (tr); head1=sprintf('BY SUCCESSIVE APPROXIMATION METHOD:'); disp(head1); head=sprintf('Time \t\t\t\t\tepsilon_a \t\t\t\t\t epsilon_t \t\t\t\t\t

epsilon_s '); disp(head); while (ea>=es) temp=t; t=f(t); ea=abs((t-temp)/t)*100; % Calc approximate error et=abs((tr-t)/tr)*100; % Calc true error table=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t %f \t\t\t

%f',t,ea,et,es); disp(table); end

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

Enter the function:(exp(-x)-x)

Enter number of significant digits: 2

Enter initial guess: 0

BY SUCCESSIVE APPROXIMATION METHOD:

0.567143290409784

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 4

Statement: Write down the Matlab Program using Gauss Naïve elimination method.

Solution:

Input :

clc;

clear all;

a=input('enter matrix A[]: ')

b=input('enter column matrix B[]: ')

[m,n]=size(a); % determines size of matrix.

if (m~=n) error('Matrix Must Be Square!'); end

%forward elimination

for k=1:n-1

for i=k+1:n

factor=a(i,k)/a(k,k);

for j=k:n

a(i,j)=a(i,j)-(factor*(a(k,j)));% calculates each element of matrix

A.

end

b(i)=b(i)-factor*(b(k)); % calculates each element of matrix B.

end

disp (a);

end

disp (a);

disp (b);

% backward substitution

for i=n:-1:1

x(i)=b(i)/a(i,i); % calculates values of unknown matrix.

for j=1:i-1

b(j)=b(j)-x(i)*a(j,i);

end

end

disp('VALUES ARE:')

disp(x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

enter matrix A[]: [1 -1 1 ;3 4 2 ; 2 1 1 ]

a =

1 -1 1

3 4 2

2 1 1

enter column matrix B[]: [6

9

7]

b =

6

9

7

1.0000 -1.0000 1.0000

0 7.0000 -1.0000

0 0 -0.5714

6.0000

-9.0000

-1.1429

VALUES ARE:

3.0000 -1.0000 2.0000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 5

Statement: Write down the Matlab Program using Gauss with partial pivoting method.

Solution:

Input :

clc;

clear all;

a=input('enter matrix A[]: ');

b=input('enter column matrix B[]: ');

[m,n]=size(a); % calculates size of matrix A.

if (m~=n) error('Matrix Must Be Square!'); end

%pivoting

for k=1:n-1

[xyz,i]=max(abs(a(k:n,k))); % finds maximum element in matrix A.

ipr=i+k-1;

if ipr~=k

a([k,ipr],:)=a([ipr,k],:); % interchanging of rows.

b([k,ipr],:)=b([ipr,k],:); % interchanging of rows.

end

%forward elimination

for i=k+1:n

factor=a(i,k)/a(k,k);

for j=k:n

a(i,j)=a(i,j)-(factor*(a(k,j))); % calculates each element of matrix

A.

end

b(i)=b(i)-factor*(b(k)); % calculates each element of matrix B.

end

disp (a);

end

%disp (a);

disp (b);

% backward substitution

for i=n:-1:1

x(i)=b(i)/a(i,i); % calculates values of unknown matrix.

for j=1:i-1

b(j)=b(j)-x(i)*a(j,i);

end

end

disp('VALUES ARE:')

disp(x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output : enter matrix A[]: [2 -6 -1;-3 -1 7;-8 1 -2]

enter column matrix B[]: [-38;-34;-20]

-8.00000000000000 1.00000000000000 -2.00000000000000

0 -1.37500000000000 7.75000000000000

0 -5.75000000000000 -1.50000000000000

-8.00000000000000 1.00000000000000 -2.00000000000000

0 -5.75000000000000 -1.50000000000000

0 0 8.10869565217391

-20.00000000000000

-43.00000000000000

-16.21739130434783

VALUES ARE:

4 8 -2

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 6

Statement: Write down the Matlab Program using Thomas Algorithm method.

Solution:

Input :

clc;

clear;

%format long;

e=input('Enter the value of e, ie. subdiagonal vector :');

f=input('Enter the value of f, ie. diagonal vector :');

g=input('Enter the value of g, ie. superdiagonal vector :');

r=input('Enter the value of r, ie. value vector :');

n=length(e); % Size of matrix e

for k=1:n

factor=e(k)/f(k); % Multiplying factor

f(k+1)=f(k+1)-factor*g(k); % Transforming diagonal vector

r(k+1)=r(k+1)-factor*r(k); % Transforming value vector

end

x(n+1)=r(n+1)/f(n+1); % Transforming unknown vector

for k=n:-1:1

x(k)=(r(k)-g(k)*x(k+1))/f(k); % Finding values of unknowns

end

disp('VALUES ARE:');

disp (x)

Output : Enter the value of e, ie. subdiagonal vector :[-.4;-.4]

Enter the value of f, ie. diagonal vector :[0.8;0.8;0.8]

Enter the value of g, ie. superdiagonal vector :[-.4;-.4]

Enter the value of r, ie. value vector :[41;25;105]

VALUES ARE:

173.7500 245.0000 253.7500

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 7

Statement: Write down the Matlab Program using Gauss Seidel without Relaxation method.

Solution:

Input :

clc;

clear all;

format long;

a = input('Enter Matrix A: ');

b = input('Enter Column Matrix B: ');

[m,n]= size(a); % calculates size of matrix A.

if (m~=n) error('Matrix Must Be Square!'); end

for i=1:n

d(i)=b(i)/a(i,i);

end

d=d';

c=a;

for i=1:n

for j=1:n

c(i,j)=a(i,j)/a(i,i); % factor.

end

c(i,i)=0;

x(i)=0;

end

x=x';

disp (a);

disp (b);

disp (d);

disp (c);

p = input('Enter No. of Iterations: ');

for k=1:p

for i=1:n

x(i)=d(i)-c(i,:)*x(:,1); % finds unknown value.

end

disp (x);

end

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output : Enter Matrix A: [3 -0.1 -0.2;0.1 7 -.3;0.3 -0.2 10]

Enter Column Matrix B: [7.85;-19.3;71.4]

3.00000000000000 -0.10000000000000 -0.20000000000000

0.10000000000000 7.00000000000000 -0.30000000000000

0.30000000000000 -0.20000000000000 10.00000000000000

7.85000000000000

-19.30000000000000

71.40000000000001

0 -0.03333333333333 -0.06666666666667

0.01428571428571 0 -0.04285714285714

0.03000000000000 -0.02000000000000 0

Enter No. of Iterations: 3

2.61666666666667

-2.79452380952381

7.00560952380952

2.99055650793651

-2.49962468480726

7.00029081106576

3.00003189791081

-2.49998799235305

6.99999928321562

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 8

Statement: Write down the Matlab Program using Gauss Seidel with relaxation method.

Solution:

Input :

clc;

clear all;

a=input('enter matrix A[]: ');

b=input('enter column matrix B[]: ');

[m,n]=size(a); % calculates size of matrix A.

if (m~=n) error('Matrix Must Be Square!'); end

%pivoting

for k=1:n-1

[xyz,i]=max(abs(a(k:n,k))); % finds maximum element in matrix A.

ipr=i+k-1;

if ipr~=k

a([k,ipr],:)=a([ipr,k],:); % interchanging of rows.

b([k,ipr],:)=b([ipr,k],:); % interchanging of rows.

end

end

for i=1:n

d(i)=b(i)/a(i,i);

end

d=d';

c=a;

for i=1:n

for j=1:n

c(i,j)=a(i,j)/a(i,i); % factor.

end

c(i,i)=0;

x(i)=0;

end

x=x';

disp (a);

disp (b);

disp (d);

disp (c);

lambda = input('Enter the value of weighting factor: ');

es=0.05; % stopping criteria.

ea(i)=100;

head=sprintf('\t\t\t\t\t\t\t\t\tValue of x \t\t\t\t\t\t\t\t\t\t\t\t\t\tValue

of ea ');

disp(head);

while (ea(i)>=es)

for i=1:n

y=x(i);

x(i)=d(i)-c(i,:)*x(:,1);

x(i)=lambda*x(i)+(1-lambda)*y; % calculates unknown value.

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

ea(i)=abs((x(i)-y)/x(i))*100;

end

table1=sprintf('%d \t\t\t %f\t\t\t\t %4.9f \t\t\t%f \t\t\t

%f\t\t\t\t %4.9f',x,ea);

disp(table1);

end

Output :

enter matrix A[]: [-3 1 12;6 -1 -1;6 9 1]

enter column matrix B[]: [50;3;40]

6 -1 -1

6 9 1

-3 1 12

3

40

50

0.5000

4.4444

4.1667

0 -0.1667 -0.1667

0.6667 0 0.1111

-0.2500 0.0833 0

Enter the value of weighting factor: 0.95

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Value of x Value of ea

4.750000e-001 3.921389 3.760702546 100.000000 100.000000 100.000000000

1.715081e+000 2.935111 4.321337313 72.304517 33.602766 12.973640471

1.709692e+000 2.830032 4.356407772 0.315233 3.712986 0.805031598

1.698338e+000 2.828267 4.355604413 0.668543 0.062401 0.018444248

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 42

Statement: Write down the Matlab Program to fit curve y = a0 + a1*x by using least square

techniques for given set of points.

Solution:

Input :

clc;

clear all;

x=input('Enter row matrix x : ');

y=input('Enter row matrix y : ');

[m,n]=size(x);

xy(1,1)=0;

i=1; X=0; Y=0; XY=0; Xsqr=0;

while i<=n;

xy(1,i)=x(1,i)*y(1,i);

xsqr(1,i)=x(1,i)^2;

X=X+x(1,i); % To calculate summation of x

Y=Y+y(1,i); % To calculate summation of y

XY=XY+xy(1,i); % To calculate summation of x*y

Xsqr=Xsqr+xsqr(1,i); % To calculate summation of x^2

i=i+1;

end

disp(x);

disp(y);

a1=(n*XY-Y*X)/(n*Xsqr-X^2);

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2);

ym=Y/n;

sr(1,1)=0;j=1;

while j<=n

sr(1,j)=(y(1,j)-a0-a1*x(1,j))^2; % To calculate sr for each x

st(1,j)=(y(1,j)-ym)^2; % To calculate st for each x

j=j+1;

end

SR=sum(sr);

ST=sum(st);

r2=(ST-SR)/ST

s=sprintf('Best fit curve (straight line) for above data is given by : y = %f

* x + %f',a1,a0);

disp(s);

xp=linspace(min(x),max(x));

yp=a0+a1*xp;

plot(x,y,'o',xp,yp);

xlabel('values of x');

ylabel('values of y');

title('y=a0+a1*x');

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

grid on;

Output :

Enter row matrix x : [1.0 2.0 3.0 4.0 5.0 6.0 7.0]

Enter row matrix y : [0.5 2.5 2.0 4.0 3.5 6.0 5.5]

1 2 3 4 5 6 7

0.5000 2.5000 2.0000 4.0000 3.5000 6.0000 5.5000

r2 =

0.8683

Best fit curve (straight line) for above data is given by :

y = 0.839286 * x + 0.071429

1 2 3 4 5 6 70

1

2

3

4

5

6

values of x

valu

es o

f y

y=a0+a1*x

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 9

Statement: Write down the Matlab Program to fit curve y = a0 + a1*x+a2x2 by using least square

techniques for given set of points.

Solution:

Input :

clc;

clear all;

x = input('Enter values of x in row matrix form : ');

y = input('Enter values of y in row matrix form : ');

[m,n]=size(x);

sx = sum(x);

sy = sum(y);

sx2 = sum(x.*x);

sxy = sum(x.*y);

sx2y = sum(x.*x.*y);

sx3 = sum(x.*x.*x);

sx4 = sum(x.*x.*x.*x);

a = [sx2 sx n; sx3 sx2 sx; sx4 sx3 sx2];

b = [sy; sxy; sx2y];

z=inv(a)*b;

s=sprintf('Best fit curve (Quadratic) for above data is given by :y = %f + %f

* x + %f * x^2 ',z(1),z(2),z(3));

disp(s);

xp = linspace(min(x),max(x));

yp = z(3)*(xp.*xp)+z(2)*xp+z(1);

plot(x,y,'o',xp,yp);

grid on;

xlabel('Values of x');

ylabel('Values of function');

title('y=a0+ a1*x+ a2*(x^2)');

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

Enter values of x in row matrix form : [0.075 0.5 1 1.2 1.7 2.0 2.3]

Enter values of y in row matrix form : [600 800 1200 1400 2050 2650 3750]

Best fit curve (Quadratic) for above data is given by :y = 643.601494 + -

218.884701 * x + 685.248397 * x^2

0 0.5 1 1.5 2 2.5500

1000

1500

2000

2500

3000

3500

4000

Values of x

Valu

es o

f fu

nction

y=a0+ a1*x+ a2*(x2)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 10

Statement: Write down the Matlab Program to fit curve y = a1*(xb1) by using least square

techniques for given set of points.

Solution:

Input :

clc;

clear all;

xa=input('Enter row matrix x : ');

ya=input('Enter row matrix y : ');

[m,n]=size(xa);

xy(1,1)=0; y(1,1)=0;

i=1; X=0; Y=0; XY=0; Xsqr=0;

while (i<=n)

y(1,i)=log10(ya(1,i)); % To calculate log of y

x(1,i)=log10(xa(1,i)); % To calculate log of x

xy(1,i)=x(1,i)*y(1,i);

xsqr(1,i)=x(1,i)^2;

X=X+x(1,i); % To calculate summation of x

Y=Y+y(1,i); % To calculate summation of y

XY=XY+xy(1,i); % To calculate summation of x*y

Xsqr=Xsqr+xsqr(1,i); % To calculate summation of x^2

i=i+1;

end

disp(xa);

disp(ya)

beta=(n*XY-Y*X)/(n*Xsqr-X^2);

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2);

alpha=10^(a0); % To calculate co-eff of x^a0

ym=Y/n;

sr(1,1)=0;j=1;

while j<=n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2; % To calculate sr for each x

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

st(1,j)=(y(1,j)-ym)^2; % To calculate st for each x

j=j+1;

end

SR=sum(sr);

ST=sum(st);

r2=(ST-SR)/ST

s=sprintf('Best fit curve (polynomial) for above data is given by : y = %f *

x^(%f) ',alpha,beta);

disp(s);

xp = linspace(min(x),max(x));

yp = (xp.^beta)*alpha;

plot(xa,ya,'o')

hold on

plot(xp,yp)

grid on;

xlabel('values of x');

ylabel('values of y');

title('y=alpha*x^(beta)');

Output :

Enter row matrix x : [26.67 93.33 148.89 315.56]

Enter row matrix y : [1.35 0.085 0.012 0.00075]

26.6700 93.3300 148.8900 315.5600

1.3500 0.0850 0.0120 0.0008

r2 =

0.9757

Best fit curve (polynomial) for above data is given by :

y = 38147.936083 * x^(-3.013376)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

0 50 100 150 200 250 300 3500

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

values of x

valu

es o

f y

y=alpha*(x)b

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 41

Statement: Write down the Matlab Program to fit curve y = a1 * e (b1*x) by using least square

techniques for given set of points.

Solution:

Input :

clc;

clear all;

x=input('Enter row matrix x : ');

ya=input('Enter row matrix y : ');

[m,n]=size(x); % Defining size of matrix x

xy(1,1)=0; y(1,1)=0; % Defining matrix xy & y

i=1; X=0; Y=0; XY=0; Xsqr=0; % Setting initial condition for loop

while i<=n;

y(1,i)=log(ya(1,i));

xy(1,i)=x(1,i)*y(1,i);

xsqr(1,i)=x(1,i)^2;

X=X+x(1,i);

Y=Y+y(1,i);

XY=XY+xy(1,i);

Xsqr=Xsqr+xsqr(1,i);

i=i+1;

end

disp(x);

disp(ya);

a1=(n*XY-Y*X)/(n*Xsqr-X^2);

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2);

alpha=exp(a0);

ym=Y/n; % Finding mean

sr(1,1)=0;j=1;

while j<=n;

sr(1,j)=(y(1,j)-a0-a1*x(1,j))^2;

st(1,j)=(y(1,j)-ym)^2;

j=j+1;

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

end

xp = linspace(min(x),max(x)); % Condition for graph

yp= alpha*exp(a1*xp); % Given function

SR=sum(sr);

ST=sum(st);

r2=(ST-SR)/ST % Co-efficient of determination

s=sprintf('Best fit curve (exponential) for above data is given by : y = %f *

e^(%f * x) ',alpha,a1);

disp(s);

plot(x,ya,'o',xp,yp) % Plots function & best fitted curve simultaneously

grid on;

xlabel('values of x'); % Defining specifications of graph

ylabel('values of y');

title('y=alpha*e^(beta*x)');

grid on; % To display grid on graph

Output :

Enter row matrix x : [0.4 0.8 1.2 1.6 2.0 2.3]

Enter row matrix y : [800 975 1500 1950 2900 3600]

0.4000 0.8000 1.2000 1.6000 2.0000 2.3000

800 975 1500 1950 2900 3600

r2 =

0.9933

Best fit curve (exponential) for above data is given by :

y = 546.590939 * e^(0.818651 * x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4500

1000

1500

2000

2500

3000

3500

4000

values of x

valu

es o

f y

y=alpha*e(beta*x)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 11

Statement: Write down the Matlab Program for Lagrange Interpolation.

Solution:

Input :

clc; clear all; x = input('Enter the of Values of x: '); y = input('Enter the of Values of y: '); u = input('Value of x at which y is to be evaluated: '); n = length(x); % Size of matrix x p=1; s=0; for i=1:n p=y(i); for j=1:n if (i~=j) % Condition for inequality p=p*(u-x(j))/(x(i)-x(j)); % Formula end end s=s+p; % Summation end fprintf('\n Value of y at required x is : %f ',s);

Output :

Enter the of Values of x: [1 4 5 7]

Enter the of Values of y: [21.746 438.171 1188.9147 8775.011]

Value of x at which y is to be evaluated: 4.2

Value of y at required x is : 490.360287

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 12

Statement: Write down the Matlab Program for Newton-Gregory Forward Difference

Interpolation.

Solution:

Input :

clc;

clear all;

x=input('Enter row matrix x : ');

y=input('Enter row matrix y : ');

X=input('Enter value of x at which value of function is to be calculated :

');

[m,n]=size(x);

dx=diff(x); % Spatial diff.(for equally spaced data)

d(1,1)=y(1,1);

disp(x);

disp(y);

for j=1:(n-1)

dy=diff(y); % Delta matrix

disp(dy);

d(j+1)=dy(1); % Stores 1st value of delta matrix.

y=dy;

end

alpha=(X-x(1))/dx(1); % Value of alpha is calculated.

a(1,1)=1; prod=1;

for k=1:(n-2)

prod=prod*(alpha-k+1);

a(k+1)=prod;

end

func=0;

for i=1:n-1

fx=a(i)*d(i)/(factorial(i-1));

func=func+fx;

end

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

s=sprintf('Value of function calculated by N-G forward interpolation :

%f',func);

disp(s);

Output :

Enter row matrix x : [2 3 4 5 6 7 8 9]

Enter row matrix y : [19 48 99 178 291 444 643 894]

Enter value of x at which value of function is to be calculated : 3.5

2 3 4 5 6 7 8 9

19 48 99 178 291 444 643 894

29 51 79 113 153 199 251

22 28 34 40 46 52

6 6 6 6 6

0 0 0 0

0 0 0

0 0

0

Value of function calculated by N-G forward interpolation : 70.375000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 13

Statement: Write down the Matlab Program for Newton-Gregory Backward Difference

Interpolation.

Solution:

Input :

clc;

clear all;

x=input('Enter row matrix x : ');

y=input('Enter row matrix y : ');

X=input('Enter value of x at which value of function is to be calculated :

');

[m,n]=size(x);

dx=diff(x); % Spatial diff.(for equally spaced data)

d(1,1)=y(n);

newx(1,n:-1:1)=x(1,1:n); % Reversing order of matrix x so that nth value is

brought 1st.

newy(1,n:-1:1)=y(1,1:n); % Reversing order of matrix y so that nth value is

brought 1st.

disp(newx)

disp(newy)

for j=1:(n-1)

dy=diff(newy); % Delta matrix

disp(dy);

d(j+1)=dy(1); % Stores 1st value of delta matrix.

newy=dy;

end

alpha=(x(n)-X)/dx(1); % Value of alpha is calculated.

a(1,1)=1; prod=1;

for k=1:(n-2)

prod=prod*(alpha-k+1);

a(k+1)=prod;

end

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

func=0;

for i=1:n-1

fx=a(i)*d(i)/(factorial(i-1));

func=func+fx;

end

s=sprintf('Value of function calculated by N-G backward interpolation :

%f',func);

disp(s);

Output :

Enter row matrix x : [0.1 0.2 0.3 0.4 0.5]

Enter row matrix y : [1.4 1.56 1.76 2 2.28]

Enter value of x at which value of function is to be calculated : 0.25

0.5000 0.4000 0.3000 0.2000 0.1000

2.2800 2.0000 1.7600 1.5600 1.4000

-0.2800 -0.2400 -0.2000 -0.1600

0.0400 0.0400 0.0400

1.0e-015*0.2220 -0.2220

-4.4409e-016

Value of function calculated by N-G backward interpolation : 1.655000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 14

Statement: Write down the Matlab Program for Hermite interpolation method.

Solution:

Input :

clc;

clear all;

disp('HERMITE INTERPOLATION');

x=input('Enter the values of x: ');

xu=input('Enter the value unknown of x: ');

fx=input('Enter the values of fx: ');

dfx=input('Enter the values of dfx: ');

n=size(x); % Size of matrix

sum=0;suma=0;sumb=0;

for i=1:n

pro=1;

pro1=1;

for j=1:n

if i~=j

pro=pro*(xu-x(j))/(x(i)-x(j)); % Lagrange formulation of unknown x.

pro1=pro1*(x(i)-x(j)); % Derivative of Lagrange term

end

end

L(i,1)=pro; % Lagrange term

dL(i,1)=pro1; % Derivative of Lagrange term

end

for k=1:n

suma=suma+(1-2*(xu-x(k))*dL(k))*((L(k))^2)*fx(k); % Summation

sumb=sumb+(xu-x(k))*((L(k))^2)*dfx(k);

end

sumf=suma+sumb;

disp('The value of fx at unknown x is: ');

disp(sumf);

Output:

HERMITE INTERPOLATION

Enter the values of x: [0;1]

Enter the value unknown of x: 0.4

Enter the values of fx: [0;1]

Enter the values of dfx: [0;2]

The value of fx at unknown x is:

0.1600

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 15

Statement: Write down the Matlab Program for interpolation by Cubic spline.

Solution:

Input :

% Clearing Workspace

clear all;

clc;

close;

% Defining Input points

x1=input('Enter matrix for values of x: ');

y1=input('Enter matrix for values of y: ');

xg=input('Enter value of x for which to find y: ');

m1=size(x1);

n=m1(1,2);

x=x1'; y=y1';

scatter(x,y);

hold on;

% MATLAB function plotting Cubic Interpolation

yy = spline(x,y,0:0.01:100);

plot(x,y,'o',0:0.01:100,yy);

% Defining end conditions f''(x)=0 @ 1st and last point

M(1:n+1)=0;

% First row of matrix to be solved

A(1,1:3)=[2*(x(3)-x(1)) (x(3)-x(2)) 0];

B(1,1)=6*(y(3)-y(2))/(x(3)-x(2))-6*(y(2)-y(1))/(x(2)-x(1));

% Subsequent rows till n-2

if n>3

for l=2:n-2

A(l,l-1:l+1)=[(x(l+1)-x(l)) 2*(x(l+2)-x(l)) (x(l+2)-x(l+1))];

B(l,1)=6*(y(l+2)-y(l+1))/(x(l+2)-x(l+1))-6*(y(l+1)-y(l))/(x(l+1)-

x(l));

end

end

% Last 1 row

A(n-1,n-2:n-1)=[(x(n)-x(n-1)) 2*(x(n)-x(n-1))];

B(n-1,1)=-6*(y(n)-y(n-1))/(x(n)-x(n-1));

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

% Finding other values of f''(x)

N=GaussSoln(A,B);

% Assigning Values to M

for i=1:n-1

M(i+1)=N(i);

end

% Creating the interpolation function between intervals

f=inline('Ma/6/(xb-xa)*(xb-xx)^3-Mb/6/(xb-xa)*(xa-xx)^3+(ya/(xb-xa)-Ma*(xb-

xa)/6)*(xb-xx)-(yb/(xb-xa)-Mb*(xb-xa)/6)*(xa-

xx)','xx','Ma','Mb','xa','xb','ya','yb');

% Ploting the spline in intervals

xn(1:1000)=0;

yn(1:1000)=0;

for i=1:n-1

j=1;

dx=(x(i+1)-x(i))/1000;

for k=x(i):dx:x(i+1)

xn(j)=k;

yn(j)=f(k,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1));

j=j+1;

end

if xg>=x(i) && xg<=x(i+1)

yg=f(xg,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1));

end

plot(xn,yn, 'LineWidth',2);

xlim([min(x) max(x)]);

ylim([min(y) max(y)]);

end

hold off;

fprintf('@x=%f, y=%f\n',xg,yg);

GaussSoln:

function Soln=GaussSoln(x,y)

A1=x;

B=y;

n2=size(B);

n=n2(1,1);

clear x;

clear y;

if det(A1)==0

disp('Either no solution or infinitely many solutions.');

else

A=A1;

A(:,n+1)=B(1:n);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

for i=1:n-1

for j=i:n-1

fac=A(j+1,i)/A(i,i);

fac_mat=fac*A(i,:);

A(j+1,:)=A(j+1,:)-fac_mat;

end

end

i=0;j=0;

if A(n,n)==0

an(n)=0;

else

an(n)=A(n,n+1)/A(n,n);

end

for i=n-1:-1:1

for j=n:-1:1

x(j)=an(j)*A(i,j);

end

y=sum(x);

if y==0

an(i)=0;

else

an(i)=(A(i,n+1)-y)/A(i,i);

end

end

end

Soln=an;

Output:

Enter matrix for values of x: [1 2 3 4]

Enter matrix for values of y: [0 0.3 0.48 0.6]

Enter value of x for which to find y: 2.3

@x=2.300000, y=0.363014

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 16

Statement: Write down the Matlab Program for Inverse Interpolation.

Solution:

Input :

clc;

clear all; x = input('Enter the of Values of x: '); y = input('Enter the of Values of y: '); r = input('Value of y at which x is to be evaluated: '); n = length(x); % determines size of matrix.

p=1; s=0; for j=1:n for i=1:n if i==j continue; end numerator=r-y(i); denominator=y(j)-y(i); v(j)=numerator/denominator; p=p*v(j); end s=s+p*x(j); p=1; end fprintf('\n Value is : %f ',s)

Output :

Enter the of Values of x: [0 1 2 3]

Enter the of Values of y: [0 1 7 25]

Value of y at which x is to be evaluated: 2

Value is : 1.716138

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 17

Statement: Write down the Matlab Program for Newton Forward Differentiation.

Solution:

Input :

clc; clear all; x=input('Enter row matrix x : '); y=input('Enter row matrix y : '); r=input('Enter value of x at which value of function is to be calculated :

'); [m,n]=size(x); p=1; h=diff(x); % Step size disp(x); disp(y); for j=1:n if (r==x(j)) p=j; end end d(1,1)=y(1,p); for j=1:(n-p) dy=diff(y); % Delta matrix disp(dy); y=dy; d(j+1)=y(1,p); % Stores p th value of delta matrix. end f=0; for k=1:n-1 fr=d(k+1)/k; f=f+((-1)^(k-1))*fr; end dx=(f/h(1)); s=sprintf('Value of dy/dx at %f is : % f',r,dx); disp (s);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

Enter row matrix x : [1.5 2 2.5 3 3.5 4]

Enter row matrix y : [3.375 7 13.625 24 38.875 59]

Enter value of x at which value of function is to be calculated : 1.5

1.5000 2.0000 2.5000 3.0000 3.5000 4.0000

3.3750 7.0000 13.6250 24.0000 38.8750 59.0000

3.6250 6.6250 10.3750 14.8750 20.1250

3.0000 3.7500 4.5000 5.2500

0.7500 0.7500 0.7500

0 0

0

Value of dy/dx at 1.500000 is : 4.750000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 18

Statement: Write down the Matlab Program for Newton Backward Differentiation.

Solution:

Input :

clc; clear all; xin=input('Enter row matrix x : '); yin=input('Enter row matrix y : '); r=input('Enter value of x at which value of function is to be calculated :

'); [m,n]=size(xin); p=1; h=diff(xin); % Step size y(1,n:-1:1)=yin(1,1:n); % Reversing order of matrix y so that nth value is brought

1st. x(1,n:-1:1)=xin(1,1:n); % Reversing order of matrix x so that nth value is brought

1st.

disp(x) disp(y) for j=1:n if (r==x(j)) p=j; end end d(1,1)=y(1,p); for j=1:(n-p) dy=diff(y); % Delta matrix y=(-1)*dy; d(j+1)=(y(1,p)); % Stores p th value of delta matrix. disp(y); end f=0; for k=1:n-1 fr=d(k+1)/k; f=f+fr; end dx=(f/h(1)); s=sprintf('Value of dy/dx at %f is : % f',r,dx); disp (s);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

Enter row matrix x : [0 10 20 30 40]

Enter row matrix y : [1 0.984 0.939 0.866 0.766]

Enter value of x at which value of function is to be calculated : 40

40 30 20 10 0

0.7660 0.8660 0.9390 0.9840 1.0000

-0.1000 -0.0730 -0.0450 -0.0160

-0.0270 -0.0280 -0.0290

0.0010 0.0010

-2.2204e-016

Value of dy/dx at 40.000000 is : -0.011317

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 19

Statement: Write down the Matlab Program using Trapezoidal rule(single segment) for any

function.

Solution:

Input :

clear; clc; fprintf('NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=2; % No. of points y=inline(x); % Defining function h=(b-a)/n; % Step size S=0; for i=1:n-1; t=2*y(a+i*h); S=S+t; end A=h/2*(y(a)+y(b)+S); % Calculation of area fprintf('\nAnswer= %f\n',A);

Output :

NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE

Enter a function to integrate f(x)=4*x+2

Enter Lower Limit: 1

Enter Upper Limit: 4

Answer= 36.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 20

Statement: Write down the Matlab Program using Trapezoidal rule(multiple segment) for any

function.

Solution:

Input :

clear; clc; fprintf('NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=input('enter no. of segments'); y=inline(x); % Defining function h=(b-a)/n; % Step size S=0; for i=1:n-1; t=2*y(a+i*h); S=S+t; end A=h/2*(y(a)+y(b)+S); % Calculation of area fprintf('\nAnswer= %f\n',A);

Output :

NUMERICAL INTEGRATION BY TRAPEZOIDAL RULE

Enter a function to integrate f(x)=4*x+2

Enter Lower Limit: 1

Enter Upper Limit: 4

enter no. of segments6

Answer= 36.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 21

Statement: Write down the Matlab Program using Simpson’s 1/3rd (single segment) rule for any

function.

Solution:

Input :

clear; clc; fprintf('NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); n=2; % No. of segment y=inline(x); h=(b-a)/n; S=0; for i=1:n-1; if mod(i,2)==1 % Condition for even segments t=4*y(a+i*h); else t=2*y(a+i*h); end S=S+t; end A=h/3*(y(a)+y(b)+S); fprintf('\nAnswer= %f\n',A);

Output : NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE

Enter a function to integrate f(x)=exp(x)

Enter Lower Limit: 0

Enter Upper Limit: 4

Answer= 44.247402

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 22

Statement: Write down the Matlab Program using Simpson’s 1/3rd (multiple segment) rule for

any function.

Solution:

Input :

clear;

clc;

fprintf('NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE \n\n');

x=input('Enter a function to integrate f(x)=','s');

a=input('Enter Lower Limit: ');

b=input('Enter Upper Limit: ');

n=input(‘Enter no. of divisions: ’);

y=inline(x);

h=(b-a)/n;

S=0;

for i=1:n-1;

if mod(i,2)==1

t=4*y(a+i*h);

else

t=2*y(a+i*h);

end

S=S+t;

end

A=h/3*(y(a)+y(b)+S);

fprintf('\nAnswer= %f\n',A);

Output :

NUMERICAL INTEGRATION BY SIMPSONS 1/3 RULE

Enter a function to integrate f(x)=exp(x)

Enter Lower Limit: 0

Enter Upper Limit: 4

enter no.of divisions:5

Answer= 44.683772

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 23

Statement: Write down the Matlab Program using Simpson’s 3/8th rule for any function.

Solution:

Input :

clear;

clc;

fprintf('NUMERICAL INTEGRATION BY SIMPSONS 3/8 RULE \n\n');

x=input('Enter a function to integrate f(x)=','s');

a=input('Enter Lower Limit: ');

b=input('Enter Upper Limit: ');

while mod(n,3)~=0 % Condition for no. of segments

n=input('Enter No. of Divisions [Should be divisible by 3]: ');

end

y=inline(x); % Defining function

h=(b-a)/n; % Step size

S=0;

for i=1:n-1;

if mod(i,3)==0 % Decision statement for usage of formula

t=2*y(a+i*h);

else

t=3*y(a+i*h);

end

S=S+t;

end

A=3*h/8*(y(a)+y(b)+S); % Area calculation

fprintf('\nAnswer= %f\n',A);

Output :

Enter the function: 4*x-1

Initial Value of x :1

Final Value of x :4

Enter No. of Divisions [Should be divisible by 3]: 3

Answer: 27.000000>>

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 24

Statement: Write down the Matlab Program for Combined Simpson’s Rule.

Solution:

Input :

clear; clc; j=1; fprintf('NUMERICAL INTEGRATION BY MULTIPLE SIMPSONS RULE \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); while j==1 n=input('Enter No. of Divisions [(n-3) divisible by 2]: '); % Condition

for no. of segments if mod(n-3,2)==0 j=0; end end y=inline(x); h=(b-a)/n; S=0; if n>=3 for i=1:2; t=3*y(a+i*h); S=S+t; end A=3*h/8*(y(a)+y(a+3*h)+S); end S=0; for i=4:n-1; if mod(i,2)==0 t=4*y(a+i*h); else t=2*y(a+i*h); end S=S+t; end A=A+h/3*(y(a+3*h)+y(b)+S);

fprintf('\nAnswer= %f\n',A);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

OUTPUT: NUMERICAL INTEGRATION BY MULTIPLE SIMPSONS RULE

Enter a function to integrate f(x)=x^0.1*(1.2-x)*(1-exp(20*(x-1)))

Enter Lower Limit: 0

Enter Upper Limit: 2

Enter No. of Divisions [(n-3) divisible by 2]: 5

Answer= 55501691.391968

>>

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 25

Statement: Write down the Matlab Program for Gauss-Legendre 2-pt method.

Solution:

Input :

clear; clc; fprintf('NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 2-POINT FORMULA \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); f=inline(x); % Defining function c=(b-a)/2; % Constants d=(b+a)/2; % Constants x1=c/sqrt(3)+d; x2=-c/sqrt(3)+d; y1=f(x1); y2=f(x2); A=(y1+y2)*c; fprintf('\nAnswer= %f\n',A);

OUTPUT:

NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 2-POINT FORMULA

Enter a function to integrate f(x)=x^3+x-1

Enter Lower Limit: 1

Enter Upper Limit: 4

Answer= 68.250000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 26

Statement: Write down the Matlab Program using Gauss Legendre 3-pt rule for any function.

Solution:

Input :

clear; clc; fprintf('NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 3-POINT FORMULA \n\n'); x=input('Enter a function to integrate f(x)=','s'); a=input('Enter Lower Limit: '); b=input('Enter Upper Limit: '); f=inline(x); % Defining function c=(b-a)/2; d=(b+a)/2; x1=c*sqrt(3/5)+d; x2=-c*sqrt(3/5)+d; x3=d; y1=f(x1); y2=f(x2); y3=f(x3); A=(5/9*y1+5/9*y2+8/9*y3)*c; fprintf('\n Answer= %f\n',A);

Output :

NUMERICAL INTEGRATION BY GAUSS-LEGENDRE 3-POINT FORMULA

Enter a function to integrate f(x)=x^2-5*x+2

Enter Lower Limit: 3

Enter Upper Limit: 5

Answer= -3.333333

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 27

Statement: Write down the Matlab Program using Double integration by trapezoidal rule for

any function.

Solution:

Input :

clear; clc; % Taking Input

fprintf('DOUBLE INTEGRATION BY TRAPEZOIDAL RULE \n\n'); xy=input('Enter a function to integrate f(x,y)=','s'); ax=input('Enter Lower Limit of x: '); bx=input('Enter Upper Limit of x: '); ay=input('Enter Lower Limit of y: '); by=input('Enter Upper Limit of y: '); nx=input('No. of intervals for integration w.r.t. x: '); ny=input('No. of intervals for integration w.r.t. y: '); % Defining the function f=inline(xy); % Main Calculations

h=(bx-ax)/nx; k=(by-ay)/ny; an=0; for i=0:nx-1 for j=0:ny-1

tr=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+1)*k)+f(ax+(i+1)*

h,ay+j*k); an=an+tr; end end A=h*k/4*an; fprintf('\nAnswer= %f\n',A);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

DOUBLE INTEGRATION BY TRAPEZOIDAL RULE

Enter a function to integrate f(x,y)=x+y

Enter Lower Limit of x: 0

Enter Upper Limit of x: 2

Enter Lower Limit of y: 1

Enter Upper Limit of y: 3

No. of intervals for integration w.r.t. x: 2

No. of intervals for integration w.r.t. y: 2

Answer= 12.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 28

Statement: Write down the Matlab Program using double integration by Simpson’s 1/3rd rule

for any function.

Solution:

Input :

clear; clc; % Taking Input

fprintf('DOUBLE INTEGRATION BY SIMPSONS 1/3rd RULE \n\n'); xy=input('Enter a function to integrate f(x,y)=','s'); ax=input('Enter Lower Limit of x: '); bx=input('Enter Upper Limit of x: '); ay=input('Enter Lower Limit of y: '); by=input('Enter Upper Limit of y: '); nx=3; ny=3; while mod(nx,2)~=0 || mod(ny,2)~=0 nx=input('No. of intervals for integration w.r.t. x (Should be even): '); ny=input('No. of intervals for integration w.r.t. y (Should be even): '); end % Defining the function

f=inline(xy); % Main Calculations

h=(bx-ax)/nx; k=(by-ay)/ny; an=0; for i=0:2:nx-1 for j=0:2:ny-1

tr1=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+2)*k)+f(ax+(i+2)

*h,ay+j*k);

tr2=f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+1)*k)+f(

ax+(i+1)*h,ay+j*k); tr3=f(ax+(i+1)*h,ay+(j+1)*k); an=an+tr1+4*tr2+16*tr3; end end A=h*k/9*an; fprintf('\nAnswer= %f\n',A);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output :

DOUBLE INTEGRATION BY SIMPSONS 1/3rd RULE

Enter a function to integrate f(x,y)=x-y+1

Enter Lower Limit of x: 6

Enter Upper Limit of x: 14

Enter Lower Limit of y: 1

Enter Upper Limit of y: 5

No. of intervals for integration w.r.t. x (Should be even): 4

No. of intervals for integration w.r.t. y (Should be even): 4

Answer= 256.000000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 29

Statement: Write down the Matlab Program for Euler Method.

Solution:

Input : clc; clear all;

dydx=input('Emter A Function dy/dx : ','s');

x0=input('Enter The Initial Value of x :');

y0=input('Enter The Initial Value of y :');

xf=input('Enter Value of "x" At Which Value of "y" Is To Be Found: ');

h=input('Enter Step Size :');

f=inline(dydx); % Defining function

n=(xf-x0)/h;

for i=1:n

y(i) = y0 + h*(f(x0,y0)); % Evaluating function at given x & y

y0 = y(i);

x0 = x0 + h;

end

s=sprintf('\n Value of y At x = %f Is : %f',xf,y(n));

disp(s);

Output :

Enter A Function dy/dx : (x+y)/((y^2)-(sqrt(x*y)))

Enter The Initial Value of x :1.3

Enter The Initial Value of y :2

Enter Value of "x" At Which Value of "y" Is To Be Found: 1.8

Enter Step Size :.05

Value of y At x = 1.800000 Is : 2.578164

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 30

Statement: Write down the Matlab Program for Heun’s method.

Solution:

Input :

clc;

clear all; disp('HEUNS METHOD');

format long; dydx=input('\nEnter The Function dy/dx : ','s'); x0=input('Enter The Initial Value of x: '); y0=input('Enter Initial Value of y: '); h=input('Enter step size: '); xf=input('Enter Value of x For Which y Is To Be Found: '); fprintf('\n');

f=inline(dydx); n=(xf-x0)/h;

for i=1:n yf = y0 + h*f(x0,y0); yff = y0 + h*(f(x0,y0) + f(x0+h,yf))/2; y0 = yff; x0 = x0 + h; s = sprintf('Value y = %f At x%d',yff,i); disp(s); end

Output :

HEUNS METHOD

Enter The Function dy/dx : 4*exp(.8*x) - .5*y

Enter The Initial Value of x: 0

Enter Initial Value of y: 2

Enter step size: 1

Enter Value of x For Which y Is To Be Found: 4

Value y = 6.701082 At x1

Value y = 16.319782 At x2

Value y = 37.199249 At x3

Value y = 83.337767 At x4

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 31

Statement: Write down the Matlab Program for Modified Euler method.

Solution:

Input: clc; clear all; % Clears the workspace

disp('MODIFIED EULER METHOD');

format long

% Take the input from user

eq=input('\nEnter the diff. eqn in x and y: ','s');

s=inline(eq);

y0=input('Enter y: ');

x0=input('Enter x: ');

xu=input('Enter unknown x: ');

acc=input('Enter accuracy required: ');

% Calculatoins

h=(xu-x0)/2;n=2;

for i=1:n

x1=x0+h;

y1=y0+h*s(x0,y0);

y1n=y0+(h/2)*(s(x0,y0)+s(x1,y1));

dy=abs(y1-y1n);

while dy>acc

y1=y1n;

y1n=y0+(h/2)*(s(x0,y0)+s(x1,y1));

dy=abs(y1-y1n);

end

x0=x1;

y0=y1n;

end

% Prints the answer

disp('The value of the diff eqn at unkown x is: ');

disp(y1n);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output:

MODIFIED EULER METHOD

Enter the diff. eqn in x and y: sqrt(x+y)

Enter y: 2.2

Enter x: 1

Enter unknown x: 1.2

Enter accuracy required: 0.0001

The value of the diff eqn at unkown x is: 2.573186212370175

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 32

Statement: Write down the Matlab Program for Runge-Kutta 2nd order method.

Solution:

Input: clc; clear all; % Clears the workspace

disp('RUNGE KUTTA METHOD 2ND ORDER');

format long

% Takes the input from user

eq=input('Enter the diff. eqn in x and y: ','s');

s=inline(eq); % Converts the i/p string into symbolic function

y0=input('Enter y: ');

x0=input('Enter x: ');

xu=input('Enter unknown x: ');

h=input('Enter step size: ');

n=(xu-x0)/h;

for i=1:n+1

x1=x0+h;

y1=y0+h*s(x0,y0);

c1=h*s(x0,y0);

c2=h*s(x1,y1);

c=(c1+c2)/2;

yans=y0+c;

y0=yans;

x0=x1;

end

% Prints the answer

disp('The value of the diff eqn at unkown x is: ');

disp(yans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output:

RUNGE KUTTA METHOD 2ND ORDER

Enter the diff. eqn in x and y: -(y+x*y^2)

Enter y: 1

Enter x: 0

Enter unknown x: 0.3

Enter step size: 0.1

The value of the diff eqn at unkown x is:

0.715327926979073

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 33

Statement: Write down the Matlab Program for Runge-Kutta 4th order method.

Solution:

Input: clc; clear all; % Clears the workspace

disp('RUNGE KUTTA METHOD 4TH ORDER');

format long

% Takes the input from user

eq=input('Enter the diff. eqn in x and y: ','s');

s=inline(eq); % Converts the i/p string into symbolic function

y0=input('Enter y: ');

x0=input('Enter x: ');

xu=input('Enter unknown x: ');

h=input('Enter step size: ');

% Calculation

n=(xu-x0)/h;

for i=1:n

x1=x0+h;

y1=y0+h*s(x0,y0);

c1=h*s(x0,y0);

c2=h*s((x0+(h/2)),(y0+(c1/2)));

c3=h*s((x0+(h/2)),(y0+(c2/2)));

c4=h*s(x1,(y0+c3));

c=(c1+2*c2+2*c3+c4)/6;

yans=y0+c;

y0=yans;

x0=x1;

end

% Prints the answer

disp('The value of the diff eqn at unkown x is: ');

disp(yans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output:

RUNGE KUTTA METHOD 4TH ORDER

Enter the diff. eqn in x and y: 0*x+y

Enter y: 2

Enter x: 0

Enter unknown x: 0.2

Enter step size: 0.1

The value of the diff eqn at unkown x is:

2.442805141701389

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 34

Statement: Write down the Matlab Program for Milne’s correct prediction method .

Solution:

Input: clc; clear all; % Clears the workspace

disp('MILNE PREDICTION');

format long

% Take the input from user

eq=input('Enter the 1st diff. eqn in x, y: ','s');

s=inline(eq);

y=input('Enter y: ');

x=input('Enter x: ');

xu=input('Enter unknown x: ');

h=input('Enter step size: ');

%calculation

n=(xu-x(4))/h;

f1=s(x(2),y(2));

f2=s(x(3),y(3));

f3=s(x(4),y(4));

for i=1:n+1

y4pr=y(1)+(4*h/3)*(2*f1-f2+2*f3);

f4pr=s(xu-h*(n-i),y4pr);

y4cr=y(3)+(h/3)*(f2+4*f3+f4pr);

if y4pr~=y4cr

y4pr=y4cr;

y4=y4cr;

end

f4=s(xu-h*(n-i),y4);

f1=f2;f2=f3;f3=f4;

y(1)=y(2); y(3)=y(4);

yans=y4cr;

end

disp('The value of the diff eqn at unkown x is: '); disp(yans);

Output:

MILNE PREDICTION

Enter the 1st diff. eqn in x, y: x-y+1

Enter y: [0;0.1951;0.3812;0.5591]

Enter x: [1;1.1;1.2;1.3]

Enter unknown x: 1.5

Enter step size: 0.1

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

The value of the diff eqn at unkown x is:

0.893399346172840

Assignment No: 35

Statement: Write down the Matlab Program for Runge-Kutta simultaneous method.

Solution:

Input: clc; clear all; % Clears the workspace

disp('RUNGE KUTTA METHOD 4TH ORDER FOR SIMLTANEOUS EQUATONS');

format long

% Takes the input from user

eq=input('Enter the 1st diff. eqn in x, y, z: ','s');

eq1=input('Enter the 2nd diff. eqn in x, y, z: ','s');

s=inline(eq,'x','y','z'); % Converts the i/p string into symbolic function

s1=inline(eq1,'x','y','z'); % Converts the i/p string into symbolic function

y0=input('Enter y: ');

x0=input('Enter x: '); z0=input('Enter z: ');

xu=input('Enter unknown x: ');

h=input('Enter step size: ');

% Calculation

n=(xu-x0)/h;

for i=1:n

x1=x0+h;

c1=h*s(x0,y0,z0);

d1=h*s1(x0,y0,z0);

c2=h*s((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2)));

d2=h*s1((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2)));

c3=h*s((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2)));

d3=h*s1((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2)));

c4=h*s(x1,(y0+c3),(z0+d3));

d4=h*s1(x1,(y0+c3),(z0+d3));

c=(c1+2*c2+2*c3+c4)/6;

d=(d1+2*d2+2*d3+d4)/6;

yans=y0+c;

zans=z0+d;

y0=yans;

z0=zans;

x0=x1;

end

% Prints the answer

disp('The value of the diff eqn at unknown x is: ');

disp(yans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

disp('The value of the differential at unknown x is: ');

disp(zans);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output: RUNGE KUTTA METHOD 4TH ORDER FOR SIMLTANEOUS EQUATONS

Enter the 1st diff. eqn in x, y, z: x+y*z

Enter the 2nd diff. eqn in x, y, z: x^2-y^2

Enter y: 1

Enter x: 0

Enter z: 0.5

Enter unknown x: 1.2

Enter step size: 1.2

The value of the diff eqn at unknown x is:

1.352724056760832

The value of the differential at unknown x is:

-0.775714711925248

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No:

Statement: Write down the Matlab Program for Adams Bashforth.

Solution:

Input:

clear; clc; % Clears the work space

% Get the input from user g=input('Enter the function dy/dx: ','s'); x=input('Enter values of x: '); y=input('Enter values of y: ');

xg=input('Enter x at which value is to be found: '); h=input('Enter step size: ');

f=inline(g); % Convert the input string into a symbolic function

m=size(x); % Calculate the size of matrix x

% Main calculation n=(xg-x(4))/h; for i=1:n ya=y(4)+(h/24)*(-9*(f(x(1),y(1)))+(37*(f(x(2),y(2))))-

(59*(f(x(3),y(3))))+(55*(f(x(4),y(4))))); ya1=y(4)+(h/24)*((f(x(2),y(2)))-

(5*(f(x(3),y(3))))+(19*(f(x(4),y(4))))+(9*f(x(4)+h,ya))); while(ya1~=ya) ya=ya1; ya1=y(4)+(h/24)*((f(x(2),y(2)))-

(5*(f(x(3),y(3))))+(19*(f(x(4),y(4))))+(9*f(x(4)+h,ya))); end for j=1:m-1 x(j)=x(j+1); y(j)=y(j+1); end x(m)=x(m)+h; y(4)=ya1; end

fprintf('The value at given x is : %f \n',ya1); % Prints the answer

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

OUTPUT:

Enter the function dy/dx: 1+x*y^2

Enter values of x: [0 0.1 0.2 0.3]

Enter values of y: [0.2 0.3003 0.4022 0.5075]

Enter x at which value is to be found: 0.5

Enter step size: 0.1

The value at given x is : 0.740490

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 36

Statement: Write down the Matlab Program for Parabolic method.

Solution:

Input :

% Program for Parabollic Equation (Schmidt Method) clear all; clc; a=1; b=1;

% input xi=input('Enter initial value of x: '); xf=input('Enter final value of x: '); while a==1 h=input('Enter step size for x: '); co=(xf-xi)*10000/(h*10000); if mod(co,1)==0 a=0; end end ti=input('Enter initial value of t: '); tf=input('Enter final value of t: '); while b==1 k=input('Enter step size for t: '); ro=(tf-ti)*10000/(k*10000); if mod(ro,1)==0 b=0; end end s=input('For all values of x at t=0, u(x)=','s'); f=inline(s); C=input('Enter value of C: '); r=k/h^2*C^2;

% Assign side values in matrix u(1,2:co+2)=xi:h:xf; u(2:ro+2,1)=ti:k:tf; u(2:ro+2,2)=input('Enter constant value of u for x=xi: '); u(2:ro+2,co+2)=input('Enter constant value of u for x=xf: '); % Assign central values in matrix by finding them

for i=3:co+1 u(2,i)=f(u(1,i)); end

for i=3:ro+2 for j=3:co+1 u(i,j)=r*u(i-1,j-1)+(1-2*r)*u(i-1,j)+r*u(i-1,j+1); end end

% display output

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

disp(u);

Output:

Enter initial value of x: 0

Enter final value of x: 1

Enter step size for x: 0.2

Enter initial value of t: 0

Enter final value of t: 0.006

Enter step size for t: 0.002

For all values of x at t=0, u(x)=sin(pi*x)

Enter value of C: 1

Enter constant value of u for x=xi: 0

Enter constant value of u for x=xf: 0

0 0 0.2000 0.4000 0.6000 0.8000 1.0000

0 0 0.5878 0.9511 0.9511 0.5878 0

0.0020 0 0.5766 0.9329 0.9329 0.5766 0

0.0040 0 0.5655 0.9151 0.9151 0.5655 0

0.0060 0 0.5547 0.8976 0.8976 0.5547 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 37

Statement: Write down the Matlab Program for Crank-Nicholeson method.

Solution:

Input :

% Crank Nicoleson clear all; clc; a=1; b=1; c=1;

% input

xi=input('Enter initial value of x: '); xf=input('Enter final value of x: '); h=input('Enter step size for x: '); co=(xf-xi)*10000/(h*10000); ti=input('Enter initial value of t: '); tf=input('Enter final value of t: '); k=input('Enter step size for t: '); ro=(tf-ti)*10000/(k*10000); s=input('For all values of x at t=0, u(x)=','s'); f=inline(s); C=input('Enter value of C: '); r=k*C^2/h^2;

% define side values of matrix

u(1,2:co+2)=xi:h:xf; u(2:ro+2,1)=ti:k:tf; u(2:ro+2,2)=input('Enter constant value of u for x=xi: '); u(2:ro+2,co+2)=input('Enter constant value of u for x=xf: '); for i=3:co+1 u(2,i)=f(u(1,i)); end ui=u; k=1;

% find central values of matrix while c==1 && k<=1000 ui=u; for i=2:ro+1 for j=3:co+1 %u(i+1,j)=r/(2*(1+r))*(u(i+1,j-1)+u(i+1,j+1)+u(i,j-1)-2*u(i,j)-

u(i,j+1))+u(i,j)/(1+r); u(i+1,j)=1/4*(u(i+1,j-1)+u(i+1,j+1)+u(i,j-1)+u(i,j+1)); end end k=k+1; uf=(u-ui)./u; if max(max(uf))<=0.001 c=0; end end

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

disp(u);

Output:

Enter initial value of x: 0

Enter final value of x: 3

Enter step size for x: 1

Enter initial value of t: 0

Enter final value of t: .3

Enter step size for t: .1

For all values of x at t=0, u(x)=x^2

Enter value of C: 1

Enter constant value of u for x=xi: 0

Enter constant value of u for x=xf: 0

0 0 1.0000 2.0000 3.0000

0 0 1.0000 4.0000 0

0.1000 0 1.1333 0.5333 0

0.2000 0 0.2178 0.3378 0

0.3000 0 0.1046 0.0806 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 38

Statement: Write down the Matlab Program for Hyperbolic method.

Solution:

Input :

% Program to solve Hyperbolic Partial Differential Equation clear all; clc; a=1; b=1;

% input xi=input('Enter initial value of x: '); xf=input('Enter final value of x: '); h=input('Enter step size for x: '); co=(xf-xi)*10000/(h*10000); ti=input('Enter initial value of t: '); tf=input('Enter final value of t: '); k=input('Enter step size for t: '); ro=(tf-ti)*10000/(k*10000); s=input('For all values of x at t=0, u(x)=','s'); f=inline(s); C=input('Enter value of C: '); r=h/k; if r~=C error('r is not equal to C'); end

% Assign side values in matrix u(1,2:co+2)=xi:h:xf; u(2:ro+2,1)=ti:k:tf; u(2:ro+2,2)=input('Enter constant value of u for x=xi: '); u(2:ro+2,co+2)=input('Enter constant value of u for x=xf: ');

% Assign unknown values in matrix for i=3:co+1 u(2,i)=f(u(1,i)); end for i=3:co+1 u(3,i)=(u(2,i-1)+u(2,i+1))/2; end for i=4:ro+2 for j=3:co+1 u(i,j)=u(i-1,j-1)+u(i-1,j+1)-u(i-2,j); end end

% display output disp(u);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Output:

Enter initial value of x: 0

Enter final value of x: 4

Enter step size for x: 1

Enter initial value of t: 0

Enter final value of t: 2.5

Enter step size for t: 0.5

For all values of x at t=0, u(x)=(x^2)*(2-x)

Enter value of C: 2

Enter constant value of u for x=xi: 0

Enter constant value of u for x=xf: 0

0 0 1.0000 2.0000 3.0000 4.0000

0 0 1.0000 0 -9.0000 0

0.5000 0 0 -4.0000 0 0

1.0000 0 -5.0000 0 5.0000 0

1.5000 0 0 4.0000 0 0

2.0000 0 9.0000 0 -1.0000 0

2.5000 0 0 4.0000 0 0

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Assignment No: 39

Statement: Write down the Matlab Program for Elliptical method.

Solution:

Input :

clear all; clc;

% take user input u=input('Temperature of upper surface: '); l=input('Temperature of left surface: '); r=input('Temperature of right surface: '); b=input('Temperature of lower surface: '); cs=input('No. of elements in a row: '); n=cs-1; % Create a equation matrix

an(n,n)=0; for i=1:n^2 for j=1:n^2 if i==j an(i,j)=4; elseif mod(i,n)==1 && j==i+1 an(i,j)=-1; elseif j==i-n && j>0 an(i,j)=-1; elseif j==i+n && j<=n^2 an(i,j)=-1; elseif mod(i,n)==0 && j==i-1 an(i,j)=-1; elseif mod(i,n)>1 && ( j==i+1 || j==i-1 ) an(i,j)=-1; end end end so(n)=0; for i=1:n^2 if i==1 so(i)=u+l; elseif i>1 && i<n so(i)=u; elseif i==n so(i)=u+r; elseif mod(i,n)==1 && i>n && i<=n^2-n so(i)=l; elseif mod(i,n)>1 && mod(i,n)<n && i>n && i<=n^2-n so(i)=0; elseif mod(i,n)==0 && i>n && i<=n^2-n so(i)=r; elseif i==n^2-n+1

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

so(i)=l+b; elseif i>n^2-n+1 && i<n^2 so(i)=b; elseif i==n^2 so(i)=b+r; end end an so an1=an; clear an; % solve the matrix

t=GaussSoln(an1,so,n^2); k=1;

% interpret the answers for i=1:n for j=1:n t1(i,j)=t(k); k=k+1; end end t1 hold off;

% plot the answers for i=1:n for j=1:n scatter(i,j,80,[0.5 0 0],'filled'); s=sprintf('\n %1.2f',(t1(i,j))); text(j,i,s); hold on; end end axis ij; axis ([ 0 n+1 0 n+1]); hold off;

GaussSoln:

function Soln=GaussSoln(x,y,n1) A1=x; B=y; n=n1; clear x; clear y;

% Check the conditions if det(A1)==0 disp('Either no solution or infinitely many solutions.'); else

% forward elimination A=A1; A(:,n+1)=B(1:n); for i=1:n-1 for j=i:n-1 fac=A(j+1,i)/A(i,i);

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

fac_mat=fac*A(i,:); A(j+1,:)=A(j+1,:)-fac_mat; end end i=0;j=0;

% Back substitution

if A(n,n)==0 an(n)=0; else an(n)=A(n,n+1)/A(n,n); end for i=n-1:-1:1 for j=n:-1:1 x(j)=an(j)*A(i,j); end y=sum(x); if y==0 an(i)=0; else an(i)=(A(i,n+1)-y)/A(i,i); end end end

% answer

Soln=an;

Output:

Temperature of upper surface: 100

Temperature of left surface: 100

Temperature of right surface: 0

Temperature of lower surface: 0

No. of elements in a row: 3

an =

4 -1 -1 0

-1 4 0 -1

-1 0 4 -1

0 -1 -1 4

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

so =

200 100 100 0

t1 =

75.0000 50.0000

50.0000 25.0000

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Newton Raphson method

`

START

Read significant

digits ‘n’

Read function (a) Derivative of function(b)

ft=inline(a)

dft=inline(b)

Set v=1500 vr=2500 g=9.81 m=2,00,000 uf=300

epsilon_s= (0.5*10^(2-n))

epsilon_a=100

Input initial

guess

td=tx

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

print tnew

A

while epsilon_a >=epsilon_s

tnew= td-(ft(td)/dft(td))

epsilon_a= abs((tnew-td)/tnew)*100

td=tnew

print error,tnew

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Modified Newton Raphson method

START

Read function (a) Derivative of function(b) Second derivative (c)

t0=0 f=inline(a) df=inline(b) ddf=inline(c)

Read significant

digits ‘n’

epsilon_s= (0.5*10^(2-n)) epsilon_a=100 tr=fzero(inline ft)

disp tr

Input initial guess

print head

disp head

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

NO

A

print tnew

if err<=epsilon_s

disp table

while (1)

tnew=told-((fx(told)*dfx(told)/((dfx(told)^2)-(fx(told)*d2fx(told)))))

err=abs((tnew-told)/tnew)*100

epsilon_t=abs((tr-tnew)/tr)*100 told=tnew

A

YES

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Successive Approximation

START

Read function (g)

f=inline(g)

Read significant

digits ‘n’

Input initial guess

epsilon_s= (0.5*10^(2-n)) epsilon_a=100 tr=fzero(inline (g))

disp tr

set abcd

disp abcd

set head

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A disp head

A

while ea>=es

temp=t t=f(t) ea=abs((t-temp)/t)*100 et=abs((tr-t)/tr)*100

disp table

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Gauss-Naïve Elimination method

Start

Input matrices A & B

[m,n]=size [A]

NO Print “Matrix

must be

square!”

If m~=n

YES

For k=1:n-1

A

For i=k+1:n

M

For j=k:n

Factor a(i,k)/a(k,k)

9i,k

a(I,j)=a(I,j)-factor a(k,j)

b(i)=b(i)- factor*b(k)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

Display A & B

M

End

Display values of x

b(j) = b(j) - x(i)*a(j,i)

for j=1:i-1

x(i) = b(i) / a(I,i)

For i=n:-1:1

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Gauss with Partial Pivoting method

YES

NO

A

if ipr~=k

[xyz,i]=max(abs(a(k:n,k))) ipr=i+k-1;

For k=1:n-1

If m~=n

[m,n] = size (a)

Input matrices A & B

Start

Print “Matrix must be square!”

E

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

a([k,ipr],:)=a([ipr,k],:) b([k,ipr],:)=b([ipr,k],:)

B

D

A

C

for i=n:-1:1

Display A & B

b(i)=b(i)-factor*(b(k))

a(i,j)=a(i,j)-(factor*(a(k,j)))

For j=k:n

factor=a(i,k)/a(k,k)

For i=k+1:n

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

E

D

End

b(j)=b(j)-x(i)*a(j,i)

Display x

For j=1:i-1

x(i)=b(i)/a(i,i)

C

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Thomas Algorithm

START

Input matix e,f,g,r

n=length (e)

for k=1:n

factor =e(k)/f(k) f(k+1)=f(k+1)-xg(k) r(k+1)= r(k+1)-factor*r(k)

x(n+1)=r(n+1)/f(n+1)

for k=n:1

x(k)=r(k)-g(k)*x(k+1)/k

display

end

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Gauss-Seidel without Relaxation method

Start

c(i,j)=a(i,j)/a(i,i)

For j=1:n

A

Input matrices

A & B

YES

NO

B

Print “Matrix Must

Be Square!”

For i=1:n

d=d’ c=a

d(i)=b(i)/a(i,i)

For i= m:n

if (m~=n)

[m,n]=size(a)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

B

End

Display x

x(i)=d(i)-c(i,:)*x(:,1)

For i=1:n

For k=i:p

Input no. of

iterations (p)

Display A, B, C

& D

x = x’

c(i,i)=0 x(i)=0

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Gauss-Seidel with Relaxation method

YES

NO

START

[m,n]=size (a)

Input matrix A(a)

And B(b)

Print “Matrix must be square!”

If m~=n

NO

E For k = 1:n-1

[xyz,i]=max(abs(a(k:n,k)))

Ipr=i+k-1

if ipr N=K

a[(k,ipr),:]=a[(ipr,k), :]

b[(k,ipr),:]=b[(ipr,k), :]

for i=1:n

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

M D=d’; c=0

Di = b(i)/a(1,i)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

N

es=0.05 ea(i)=100 set head

Input weighing factor (lambda)

Display a,

b, c & d

X = X’

C(I,i)=0

X(i)=0

C(I,j)=a(I,j)/a(I,i)

For j=1:n

For i=1:n

M

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

E

end

Display

table

Set table

Y=x(i)

X(i)=d(i)-c(I,:)*x(:,1)

X(i)=lambda*x(i)+(1_lambda)*y

ea(i)=abs((x(i)-y)/x(i))*100

For

i=1:n

While

ea(i)>=es

Display

head

N

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Least square techniques – Linear fit.

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Enter values of

x & y in matrix

form.

n = size of matrix x

xy(1,1) = 0

i=1

X=0

Y=0

XY=0

Xsqr=0

While i< = n

xy(1,i)=x(1,i)*y(1,i)

xsqr(1,i)=x(1,i)^2

X=X+x(1,i);

X=X+x(1,i);

XY=XY+xy(1,i)

Xsqr=Xsqr+xsqr(1,i)

i = i +1

A

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

Print x & y

a1=(n*XY-Y*X)/(n*Xsqr-X^2)

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2)

ym=Y/n;

sr(1,1)=0; j=1;

While j< = n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2

st(1,j)=(y(1,j)-ym)^2

j = j +1

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

SR=sum(sr)

ST=sum(st)

r2=(ST-SR)/ST

B

Print Function

of best fitted line

END

xp=linspace(min(x),max(x));

yp=a0+a1*xp;

Plot Function

& best fitted curve

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Least square techniques – Quadratic fit.

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Enter values of

x & y in matrix

form.

n = size of matrix x

xy(1,1) = 0

i=1

X=0

Y=0

XY=0

Xsqr=0

sx = sum(x) sy = sum(y)

sx2 = sum(x.*x) sxy = sum(x.*y)

sx2y = sum(x.*x.*y) sx3 = sum(x.*x.*x)

sx4 = sum(x.*x.*x.*x)

a = [sx2 sx n; sx3 sx2 sx; sx4 sx3 sx2]

b = [sy; sxy; sx2y]

A

z=inv(a)*b

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

a0 = z(1)

a1 = z(2)

a2 = z(3)

Print Function

of best fitted line

END

xp = linspace(min(x),max(x))

yp = z(3)*(xp.*xp)+z(2)*xp+z(1)

Plot Function

& best fitted curve

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Least square techniques – Power model.

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Enter values of

x & y in matrix

form.

n = size of matrix x

xy(1,1) = 0

y(1,1) = 0

i=1

X=0

Y=0

XY=0

Xsqr=0

While i< = n

y(1,i)=log10(ya(1,i)) x(1,i)=log10(xa(1,i)) xy(1,i)=x(1,i)*y(1,i) xsqr(1,i)=x(1,i)^2

X=X+x(1,i); X=X+x(1,i);

XY=XY+xy(1,i) Xsqr=Xsqr+xsqr(1,i)

i = i +1

A

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

Print x & y

beta=(n*XY-Y*X)/(n*Xsqr-X^2)

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2)

alpha=10^(a0)

ym=Y/n;

sr(1,1)=0; j=1;

While j< = n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2

st(1,j)=(y(1,j)-ym)^2

j = j +1

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

SR=sum(sr)

ST=sum(st)

r2=(ST-SR)/ST

B

Print Function

Of best fitted curve

END

xp = linspace(min(x),max(x))

yp = (xp.^beta)*alpha

Plot Function

& best fitted curve

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Least square techniques – Exponential fit.

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Enter values of

x & ya in matrix

form.

n = size of matrix x

xy(1,1) = 0

y(1,1) = 0

i=1

X=0

Y=0

XY=0

Xsqr=0

While i< = n

y(1,i)=log(ya(1,i))

xy(1,i)=x(1,i)*y(1,i)

xsqr(1,i)=x(1,i)^2

X=X+x(1,i);

X=X+x(1,i);

XY=XY+xy(1,i)

Xsqr=Xsqr+xsqr(1,i)

i = i +1

A

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

Print x & ya

a1=(n*XY-Y*X)/(n*Xsqr-X^2)

a0=(Y*Xsqr-X*XY)/(n*Xsqr-X^2)

alpha=exp(a0)

ym=Y/n;

sr(1,1)=0; j=1;

While j< = n

sr(1,j)=(y(1,j)-a0-beta*x(1,j))^2

st(1,j)=(y(1,j)-ym)^2

j = j +1

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

SR=sum(sr)

ST=sum(st)

r2=(ST-SR)/ST

B

Print Function

Of best fitted curve

END

xp = linspace(min(x),max(x))

yp= alpha*exp(a1*xp)

Plot Function

& best fitted curve

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Lagrange interpolation.

START

Enter values of x & y in matrix form.

& Enter value of u

n = length of matrix x

p=1; s=0

dx = x(i+1) – x(i)

For i = 1 to n

p = y(i)

p=p*(u-x(j))/(x(i)-x(j))

A

For j = 1 to n

If i=~j End B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

s = s+p

A B

Print Y

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: N-G forward difference interpolation.

START

Enter values of x & y

in matrix form &

Enter value of X

n = size of matrix x d(1,1) = y(1,1)

dx = x(i+1) – x(i)

For j = 1 to (n-1)

dy = y(j+1) – y(j)

Display dy

d(j+1) = dy(1)

y = dy

A

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

alpha = (X-x(1))/dx(1)

A

For k = 1 to (n-2)

prod = prod * (alpha-k+1)

a(k+1) = prod

a(1,1)=1

prod=1

func = 0

For i = 1 to (n-1)

fx = a(i) * d(i) / (factorial(i-1))

func=func+fx

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

Print Y

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: N-G backward difference interpolation.

START

Enter values of x & y in matrix form.

& Enter value of X

n = size of matrix x

d(1,1) = y(n)

dx = x(i+1) – x(i)

newx(1,n:-1:1) = x(1,1:n)

newy(1,n:-1:1) = y(1,1:n)

For j = 1 to (n-1)

dy = y(j+1) – y(j)

Display dy

A

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

d(j+1) = dy(1)

y = dy

B

alpha = (x(n)-X)/dx(1)

a(1,1)=1

prod=1

For k = 1 to (n-2)

prod = prod * (alpha-k+1)

a(k+1) = prod

func = 0

C

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

C

Print Y

For i = 1 to (n-1)

fx = a(i) * d(i) / (factorial(i-1))

func=func+fx

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART:Hermite Interpolation

START

Accept values of x,f(x) and f’(x)

accept unknown x

n=size(x) suma=0

sumb=0 sum=0

for i=1 to n

pro=1 pro1=1

for j=1 to n

if i≠j

pro=pro*(xu-x(j))/(x(i)-x(j)) pro1=pro1*(x(i)-x(j))

L(I,1)=pro

L’(I,1)=pro1

for k=1 to n

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A B

B

suma=suma+(1-2*(xu-x(k))*dL(k))*((L(k))^2)*fx(k)

A sumb=sumb+(xu-x(k))*((L(k))^2)*dfx(k)

sumf=suma+sumb

Display sumf

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Accept pts. X1,y1

Accept unknown xg

m1=size(x1) n=m(1,2)

x=x1’ y=y1’

plot curve(x,y)

assigning pts. In yy betwn x and y with 0:0.01:100

plot (x,y) with intermediate yy

defining f’’(x)=0 for end pts.

A(1,1:3)=[2*(x(3)-x(1)) (x(3)-x(2)) 0] B(1,1)=6*(y(3)-y(2))/(x(3)-x(2))-6*(y(2)-y(1))/(x(2)-x(1))

(solving for first row)

If n>3 else

For l=2:n-2

Solve Subsequent rows till n-2 by above method

FLOWCHART: Cubic Spline Interpolation

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

D A

A D

A(n-1,n-2:n-1)=[(x(n)-x(n-1)) 2*(x(n)-x(n-1))] B(n-1,1)=-6*(y(n)-y(n-1))/(x(n)-x(n-1))

(solving the last row)

Finding other values of f’’(x) By using gauss Solution for (A,B)

Storing in N

For i=1 to n-1

M(i+1)=N(i) (assigning the values to f’’(x) from N)

Define function for cubic spline for each interval Ma/6/(xb-xa)*(xb-xx)^3-Mb/6/(xb-xa)*(xa-xx)^3+

(ya/(xb-xa)-Ma*(xb-xa)/6)*(xb-xx)-(yb/(xb-xa)-Mb*(xb-xa)/6)*(xa-xx)

xn(1:1000)=0 Yn(1:1000)=0

For i=1 to n-1

J=1 dx=(x(i+1)-x(i))/1000

for k=x(i):dx:x(i+1)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B C

xn(j)=k;

yn(j)=f(k,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1)); j=j+1

C B

else if xg>=x(i) and xg<=x(i+1)

yg=f(xg,M(i),M(i+1),x(i),x(i+1),y(i),y(i+1))

plot (xn,yn)

display xg,yg

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Inverse Interpolation

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Enter values of x & y in matrix form.

& Enter value of r

n = size of matrix x

p=1,s=0

for i= 1 to n

numerator= r-y(i)

denominator=y(j)-y(i)

v(j)=numerator/denominator

p=p*v(j)

s=s+p*x(j) p=1

for j=1 to n

if i=j

print s

End

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Newton Forward differentiation.

START

Enter values of x & y in matrix form.

& Enter value of X

n = size of matrix x

p=1

h = x(i+1) – x(i)

A

p=j

For j = 1 to n

If r=~x(j) End

d(1,1)=y(1,p)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

For k = 1 to (n-1)

fr=d(k+1)/k

f=f+((-1)^(k-1))*fr

B

For j = 1 to (n-p)

dy = y(j+1) – y(j)

Display dy

y = dy

d(j+1)=y(1,p)

f=0

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

Print dx

END

dx=(f/h(1))

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Flowchart: Newton Backward differentiation.

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Enter values of x & y

in matrix form.

& Enter value of X

n = size of matrix x

p=1

h = x(i+1) – x(i)

A

p=j

For j = 1 to n

If r=~x(j) End

d(1,1)=y(1,p)

x(1,n:-1:1)=xin(1,1:n);

y(1,n:-1:1)=yin(1,1:n)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

For k = 1 to (n-1)

fr=d(k+1)/k

f=f+fr

B

For j = 1 to (n-p)

dy = y(j+1) – y(j)

Display dy

y = dy*(-1)

d(j+1)=y(1,p)

f=0

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

Print dx

END

dx=(f/h(1))

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Trapezoidal (single segment) method

START

print statement

Read function(x) Input lower limit(a) Upper limit(b)

y=inline(x) h=(b-a)/n s=0,n=1

for i=1:n-1

t=2*y(a+i*h) s=s+t

A=h*(y(a)+y(b)+s)

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Trapezoidal(multiple segment) method

START

print statement

Read function(x) Input lower limit(a) Upper limit(b) No. of divisions(n)

y=inline(x) h=(b-a)/n s=0

for i=1:n-1

t=2*y(a+i*h) s=s+t

A=h*(y(a)+y(b)+s)

print A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Simpson’s One Third Rule(single segment) for Integration

Start

print

statement

Read function(x) & input lower limit(a) &

input upper limit (b)

if

mod(I,2)==1

y=inline (x)

h=(b-a)/n

s=0,n=2

for i=1:n-1

M

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

YES

NO M

END

print A

A=h*(y(a)+y(b)+s)/3

s=s+t

t= 4*y(a+i*h)

t=2*y(a+i*h)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Simpson’s One Third Rule(multiple segment) for Integration

Start

print

statement

if

mod(I,2)==1

Read function(x) & input lower limit(a) &

input upper limit (b) & no. of divisions n

while

mod(n,2)~=0

input no. of division

divisible by 2

y=inline (x)

h=(b-a)/n

s=0

for i=1:n-1

M

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

YES

NO M

END

print A

A=h*(y(a)+y(b)+s)/3

s=s+t

t= 4*y(a+i*h)

t=2*y(a+i*h)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Simpson’s Three Eight Rule for Integration

Start

print

statement

if

mod(I,3)==1

Read function(x) & input lower limit(a) &

input upper limit (b) & no. of divisions n

while

mod(n,3)~=0

input no. of division

divisible by 3

y=inline (x)

h=(b-a)/n

s=0

for i=1:n-1

M

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A=3*h*(y(a)+y(b)+s)/8

YES

NO M

END

print A

s=s+t

t= 4*y(a+i*h)

t=2*y(a+i*h)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Gauss-Legendre 2-pt method

End

Start

Print A

f=inline(x)

c=(b-a)/2

d=(b+a)/2

x1=c/sqrt(3)+d

x2=-c/sqrt(3)+d

y1=f(x1)

y2=f(x2)

A=(y1+y2)*c

Read function (x)

Input lower limit a & upper limit b

Print NUMERICAL INTEGRATION BY

GAUSS LEGEDRE 2-POINT FORMULA

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Gauss-Legendre 3-pt method

End

Print A

f=inline(x)

c=(b-a)/2

d=(b+a)/2

x1=c/sqrt(3)+d

x2=-c/sqrt(3)+d

x3=d

y1=f(x1)

y2=f(x2)

y=f(x3)

A=(5/9*y1+5/9*y2+8/9*y3)*c

Start

Read function (x)

Input lower limit a & upper limit b

Print NUMERICAL INTEGRATION BY

GAUSS LEGEDREN 3-POINT FORMULA

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Double Integration by trapezoidal method

START

print statement

Read function(xy) Input lower limit of x(ax) Input lower limit of y(ay) Upper limit of x(bx) Upper limit of y(by)

Input nx Input ny

while

mod(nx,2)~=0

input no. of intervals wrt x(nx) no. of intervals wrt (ny)

f= inline(xy)

h=(bx-ax)/nx k=(by-ay)/ny an=0

for i=0:nx-1

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

B

END

tr=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+

j*k)

an=an+tr;

B

Print A

A=h*k*an/4

for j=0:ny-1

A

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Double Integration by Simpson’s One Third method

START

print statement

Read function(xy) Input lower limit of x(ax) Input lower limit of y(ay) Upper limit of x(bx) Upper limit of y(by)

nx=3 ny=3

while

mod(nx,2)~=0

input no. of intervals wrt x(nx) no. of intervals wrt (ny)

f= inline(xy)

h=(bx-ax)/nx k=(by-ay)/ny an=0

for i=0:2:nx-1

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

A

A

for j=0:2:ny-1

tr1=f(ax+i*h,ay+j*k)+f(ax+i*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+j*k) tr2=f(ax+i*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+(j+2)*k)+f(ax+(i+2)*h,ay+(j+1)*k)+f(ax+(i+1)*h,ay+j*k) tr3=f(ax+(i+1)*h,ay+(j+1)*k) an=an+tr1+4*tr2+16*tr3

B

END

Print A

A=h*k/9*an

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Euler method

START

Accept the function f(x,y)

Get inputs of initial values x0,y0

Get step size h and unknown xu

n= (xu-x0)/h

for i=1 to n

x1=x0+h

y1=y0+h*f(x0,y0)

x0=x1

y0=y1

Displaying y1

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART:Heun’s method

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

START

Accept function

Accept x0,y0,h

and unknown x

n = (x-x0)/h

yf = y0 + h*f(x0,y0)

yff = y0 + h*(f(x0,y0) + f(x0+h,yf))/2

for i= 1 to n

y0 = yff;

x0 = x0 + h

display yff

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Modified Euler method

START

Accept the function f(x,y)

Get inputs of initial values x0,y0

n=2 and unknown xu

Get accuracy

h= (xu-x0)/n

for i=1 to n

x1=x0+h

y1=y0+h*f(x0,y0)

ynew=y0+(h/2)*(f(x0,y0)+f(x1,y1)

diff=|y1-ynew|

while diff>accuracy

A

y1=ynew

ynew= y0+(h/2)*(f(x0,y0)+f(x1,y1)

diff=|y1-ynew|

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

x0=x1

A y0=ynew

Displaying y1

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Runge-Kutta2nd order method

START

Accept the function f(x,y)

Get inputs of initial values x,y

Get step size h and unknown xu

n= (xu-x0)/h m =size(x)

for i=1 to n

ya=y3+(h/24) (-9*f(x0,y0) +37*f(x1,y1)- 59*f(x2,y2) +55*f(x3,y3)) ya1=y3+ (h/24)(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+9*f(x3+h,ya)

while ya1=!ya

ya=ya1 ya1=y3+(h/24)*(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+ 9*f(x3+h,ya);

for i=1: m

A

x(j)=x(j+1); y(j)=y(j+1);

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

x(m)=x(m)+h y3=ya1

A

Displaying yanswer

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Runge-Kutta 4th order method

START

Accept the function f(x,y)

Get inputs of initial values x0,y0

Get step size h and unknown xu

n= (xu-x0)/h

for i=1 to n

x1=x0+h

y1=y0+h*f(x0,y0)

x0=x1

y0=y1

A c1=h*s(x0,y0); c2=h*s((x0+(h/2)),(y0+(c1/2))); c3=h*s((x0+(h/2)),(y0+(c2/2)));

c4=h*s(x1,(y0+c3)); c=(c1+2*c2+2*c3+c4)/6;

yanswer=y0+c

y0=yanswer

x0=x1

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

A

Displaying yanswer

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Milne’s prediction-correction order method

START

Accept the function f(x,y)

Get inputs of initial values (x0,y0),

(x1,y1), (x2,y2), (x3,y3)

Get step size h and unknown xu

n= (xu-x0)/h

f1=f(x1,y1),f2=f(x2,y2),

f3=f(x3,y3)

for i=1 to n

y4pr=y(1)+(4*h/3)*(2*f1-f2+2*f3);

f4pr=f(xu-h*(n-i),y4pr);

y4cr=y(3)+(h/3)*(f2+4*f3+f4pr)

A if y4pr≠p4cr

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

y4pr=y4cr

y4=y4cr

A f4=f(xu-h*(n-i),y4) f1=f2;f2=f3;f3=f4

y0=y1 y2=y3

yans=y4cr

yanswer=y0+c

y0=yanswer

x0=x1

Displaying yans

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Runge-Kutta for simultaneous equation

START

Accept the function f(x,y,z)

Accept the function g(x,y,z)

Get inputs of initial values x0,y0,z0

Get step size h and unknown xu

n= (xu-x0)/h

for i=1 to n

x1=x0+h

y1=y0+h*f(x0,y0)

x0=x1

y0=y1

A c1=h*f(x0,y0,z0) d1=h*g(x0,y0,z0)

c2=h*g((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2))) d2=h*sg((x0+(h/2)),(y0+(c1/2)),(z0+(d1/2))) c3=h*f((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2))) d3=h*g((x0+(h/2)),(y0+(c2/2)),(z0+(d2/2)))

c4=h*f(x1,(y0+c3),(z0+d3)) d4=h*g (x1,(y0+c3),(z0+d3))

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

c=(c1+2*c2+2*c3+c4)/6 d=(d1+2*d2+2*d3+d4)/6

yans=y0+c zans=z0+d

y0=yans

A z0=zans

x0=x1

Displaying yans

Displaying zans

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART: Adam Bashforth order method

START

Accept the function f(x,y)

Get inputs of initial values x,y

Get step size h and unknown xu

n= (xu-x0)/h m =size(x)

for i=1 to n

ya=y3+(h/24) (-9*f(x0,y0) +37*f(x1,y1)- 59*f(x2,y2) +55*f(x3,y3)) ya1=y3+ (h/24)(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+9*f(x3+h,ya)

while ya1=!ya

ya=ya1 ya1=y3+(h/24)*(f(x1,y1)-5*f(x2,y2)+19*f(x3,y3)+ 9*f(x3+h,ya);

for i=1: m

A

x(j)=x(j+1); y(j)=y(j+1);

B

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

x(m)=x(m)+h y3=ya1

A

Displaying yanswer

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B A C

FLOWCHART:PARABOLIC EQUATION START

accept x0,xn,t0,tn

accept h,k,C

accept boundary value (b)

define function u

x=x0:h:xf t=t0:k:tf

x=x’ t=t’

n=(xf-x0)/h m=(tf-t0)/k

Define matrix

A(:,1)=b A(:,n)=b

r=k/h^2*C^2

for i=2 to n

A(m+1,i)=u(x(i))

for j=m:-1:1

for i=2 to n

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A C B

A(j,i)=r*A(j+1,i-1)+(1-2*r)*A(j+1,i)+r*A(j+1,i+1)

Display A

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART:Crank-Nicolson

START

`Accept xi,xf,h,ti,tf,k

Accept function

Accept value of c

co=(xf-xi)/h ro=(tf-ti)/k

r=k*C^2/h^2 a=1,b=1.c=1

u(1,2:co+2)=xi:h:xf u(2:ro+2,1)=ti:k:t

Accept const values for:

u(2:ro+2,2)

u(2:ro+2,2)

for i=3 to co+1

u(2,i)=f(u(1,i))

ui=u;

k=1

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

B

B

while c==1 and k<=1000

ui=u;

for i=2 to ro+1

for j=3 to co+1

u(i+1,j)=1/4*(u(i+1,j-1)+u(i+1,j+1)+u(i,j-1)+u(i,j+1))

k=k+1

uf=(u-ui)./u

if max(max(uf))<=0.001

c=0

display u

END

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART:Hyperbolic Equation

Start

NO

YES error

A

If r~=C

r=h/k

Input all values of u(x) (s)

ro=(tf-ti)*10000/(k*10000)

Input Initial value of x (xi) Final value of x (xf) Step size for x (h)

Input Initial value of t (ti) Final value of t (tf) Step size for t (k)

co=(xf-xi)*10000/(h*10000)

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

A

C

A

B For j=3:co+1

For i=4:ro+2

u(3,i)=(u(2,i-1)+u(2,i+1))/2

For i=3:co+1

u(2,i)=f(u(1,i))

For i=3:co+1

Input Constant value for xi (u(2:ro+2,2)) Input Constant value for xf (u(2:ro+2,co+2)

u(1,2:co+2)=xi:h:xf u(2:ro+2,1)=ti:k:tf

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

End

A

B

u(i,j)=u(i-1,j-1)+u(i-1,j+1)-u(i-2,j)

Display u

C

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

FLOWCHART:Elliptical Equation

For i=1:n^2

Input Temperature of Upper surface (u) Left surface (l) Right surface (r) Lower surface (b) No. of elements in a row (cs)

n=cs-1 an(n,n)=0

Start

A

M

M

IF

mod(i,n)==1

&& j==i+1

(i,j)=-1

NO

YES

NO

YES an(i,j)=4

C

B

IF i==j

For

j=i:n^2

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

M

M

M

M

D

IF

mod(i,n)>1

&& j==i+1

|| j==i-1 )

A

B

an(i,j)=-1

B

so(n)=0

NO

YES

NO

YES

NO

YES

NO

YES

an(i,j)=-1

an(i,j)=-1

an(i,j)=-1

mod(i,n)==0

&& j==i-1

IF j==i+n

&& j<=n^2

IF j==i-n

&& j>0

C

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

N

N

N

N

N

so(i)=u+r

YES

YES

NO

NO

YES

NO

YES

NO

NO

YES

so(i)=u+l

so(i)=u

so(i)=l

so(i)=0

F

mod(i,n)>1 &&

mod(i,n)<n &&

i>n && i<=n^2-n

If mod(i,n)==1

&& i>n &&

i<=n^2-n

If

i==n

If i>1

&& i<n

E

If i==1

For i=1:n^2

D

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

E

E

so(i)=b+r

NO

YES

NO

YES

NO

YES so(i)=r

so(i)=b

so(i)=l+b

If

i==n^2

If i>n^2-n+1

&& i<n^2

If i==n^2-

n+1

If mod(i,n)==0

&& i>n &&

i<=n^2-n

F

N

N

N

NO

YES

N

Display

an & so

G

Third Year Mechanical Engineering Computer Oriented Numerical Methods

2011-12©

MITCOE Mechanical Engineering

End

axis ij axis ([ 0 n+1 0 n+1])

scatter(i,j,80,[0.5 0 0],'filled') s=sprintf('\n %1.2f',(t1(i,j))) text(j,i,s)

For j=1:n

For i=1:n

Display t1

t1(i,j)=t(k) k=k+1

For j=1:n

For i=1:n

an1=an clear an t=GaussSoln(an1,so,n^2) k=1

G

Recommended