Mais conteúdo relacionado
Semelhante a MITCOE 2011-12 conm-submission (20)
MITCOE 2011-12 conm-submission
- 1. 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 tttttepsilon_a tttttepsilon_t
tttttepsilon_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 ttt %ftttt %4.9f ttt %f ttt
%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
- 2. 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
- 3. 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 tttttepsilon_a tttttepsilon_t
tttttepsilon_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 ttt %ftttt %4.9f ttt %f ttt
%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
- 4. 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
- 5. 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 tttttepsilon_a ttttt epsilon_t ttttt
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 ttt %ftttt %4.9f ttt %f ttt
%f',t,ea,et,es);
disp(table);
end
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 6. 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
- 7. 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
- 8. 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
- 9. 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
- 10. 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
- 11. 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
- 12. 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
- 13. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 14. 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
- 15. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 16. 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('tttttttttValue of x ttttttttttttttValue
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
- 17. ea(i)=abs((x(i)-y)/x(i))*100;
end
table1=sprintf('%d ttt %ftttt %4.9f ttt%f ttt
%ftttt %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
- 18. 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
- 19. 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
- 20. 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
y=a0+a1*x
6
5
4
values of y
3
2
1
0
1 2 3 4 5 6 7
values of x
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 21. 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
- 22. 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
y=a0+ a1*x+ a2*(x 2)
4000
3500
3000
Values of function
2500
2000
1500
1000
500
0 0.5 1 1.5 2 2.5
Values of x
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 23. 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
- 24. 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
- 25. y=alpha*(x)b
2
1.8
1.6
1.4
1.2
values of y
1
0.8
0.6
0.4
0.2
0
0 50 100 150 200 250 300 350
values of x
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 26. 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
- 27. 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
- 28. y=alpha*e(beta*x)
4000
3500
3000
values of y
2500
2000
1500
1000
500
0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4
values of x
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 29. 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
- 30. 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
- 31. 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
- 32. 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
- 33. 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
- 34. 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
- 35. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 36. 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
- 37. % 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=%fn',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
- 38. 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
- 39. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 40. 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
- 41. 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
- 42. 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
- 43. 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
- 44. 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
- 45. 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 nn');
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= %fn',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
- 46. 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 nn');
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= %fn',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
- 47. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 48. 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 nn');
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= %fn',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
- 49. 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 nn');
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= %fn',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
- 50. 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 nn');
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= %fn',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
- 51. 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 nn');
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= %fn',A);
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 52. 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
- 53. 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 nn');
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= %fn',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
- 54. 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 nn');
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= %fn',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
- 55. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 56. 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 nn');
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= %fn',A);
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 57. 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
- 58. 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 nn');
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= %fn',A);
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 59. 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
- 60. 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
- 61. 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
- 62. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 63. 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
- 64. 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
- 65. 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
- 66. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 67. 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
- 68. 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
- 69. 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
- 70. 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
- 71. 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
- 72. 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
- 73. 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
- 74. 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
- 75. 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
- 76. 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
- 77. 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
- 78. 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
- 79. 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
- 80. 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
- 81. 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
- 82. 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
- 83. 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
- 84. 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
- 85. 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
- 86. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 87. FLOWCHART: Newton Raphson method
START
Set v=1500
vr=2500
g=9.81
m=2,00,000
uf=300
`
Read function (a)
Derivative of function(b)
ft=inline(a)
dft=inline(b)
Read significant
digits ‘n’
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
- 88. A
A
while epsilon_a >=epsilon_s
tnew= td-(ft(td)/dft(td))
epsilon_a= abs((tnew-td)/tnew)*100
td=tnew
print error,tnew
print tnew
END
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 89. 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
- 90. A
A
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
disp table
NO
if err<=epsilon_s
YES
print tnew
END
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 91. 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
- 92. A
A
disp head
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
- 93. FLOWCHART: Gauss-Naïve Elimination method
Start
Input
matrices
A&B
[m,n]=size [A]
NO Print “Matrix
If m~=n
must be
square!”
YES
For k=1:n-1
A
For i=k+1:n
Factor a(i,k)/a(k,k)
9i,k
For j=k:n
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
M
- 94. M
Display A & B
For i=n:-1:1
x(i) = b(i) / a(I,i)
for j=1:i-1
b(j) = b(j) - x(i)*a(j,i)
Display values of x
A
End
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 95. FLOWCHART: Gauss with Partial Pivoting method
Start
Input matrices A & B
[m,n] = size (a)
NO
If m~=n Print “Matrix
must be
square!”
YES
E
A For k=1:n-1
[xyz,i]=max(abs(a(k:n,k)))
ipr=i+k-1;
if ipr~=k
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 96. B
a([k,ipr],:)=a([ipr,k],:)
b([k,ipr],:)=b([ipr,k],:)
For i=k+1:n
B
factor=a(i,k)/a(k,k)
For j=k:n
a(i,j)=a(i,j)-(factor*(a(k,j)))
b(i)=b(i)-factor*(b(k))
A
Display A & B
D
for i=n:-1:1
Third Year Mechanical Engineering
C Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 97. C
x(i)=b(i)/a(i,i)
For j=1:i-1
b(j)=b(j)-x(i)*a(j,i)
D
Display x
E
End
Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 98. 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
- 99. Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
- 100. FLOWCHART: Gauss-Seidel without Relaxation method
Start
Input matrices
A&B
[m,n]=size(a)
NO Print “Matrix Must
if (m~=n)
Be Square!”
YES
For i= m:n
d(i)=b(i)/a(i,i)
d=d’
c=a
For i=1:n
B
For j=1:n
c(i,j)=a(i,j)/a(i,i) Third Year Mechanical Engineering
Computer Oriented Numerical Methods
2011-12©
MITCOE Mechanical Engineering
A