0% found this document useful (0 votes)
59 views

Numerical Method Full Matlab

This document contains 4 experiments on numerical methods using MATLAB: 1. Plotting basic MATLAB functions and operations like vectors, matrices, minimum, maximum, etc. in 2-3 sentences. 2. Solving linear and non-linear equations numerically using MATLAB functions like inv(), solve() in 1 sentence. 3. Plotting graphs of functions and their derivatives by defining x-y variables and using plot() in 2-3 sentences. 4. Plotting 3D graphs for equations like z=sin(x)+cos(y) using mesh(), surf(), meshz() etc. in 3 sentences.

Uploaded by

Samip Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Numerical Method Full Matlab

This document contains 4 experiments on numerical methods using MATLAB: 1. Plotting basic MATLAB functions and operations like vectors, matrices, minimum, maximum, etc. in 2-3 sentences. 2. Solving linear and non-linear equations numerically using MATLAB functions like inv(), solve() in 1 sentence. 3. Plotting graphs of functions and their derivatives by defining x-y variables and using plot() in 2-3 sentences. 4. Plotting 3D graphs for equations like z=sin(x)+cos(y) using mesh(), surf(), meshz() etc. in 3 sentences.

Uploaded by

Samip Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

NUMERICAL METHODS

PRACTICAL FILE

Submitted by:
SAURAV KHADKA
ROLL NO: 18

Central Department of Food Technology


Central Campus of Food Technology
Tribhuvan University
Dharan-14, Sunsari

Submitted on: (2080-01-20)


Exp. No. 1
1. MATLAB Functions:

S.N. INPUT OUTPUT S.N INPUT OUTPUT


.

1. a=[5] a= 5 19. c=ones(5,5) c =1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

2. size(a) 1 1 20. c*9 9 9 9 9 9

9 9 9 9 9

9 9 9 9 9

9 9 9 9 9

9 9 9 9 9

3. b=[1 2 3 4 5] b= 1 2 3 21. a=1:20 a =Columns 1 through


4 5 20

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

4. size(b) 1 5 22. b=1:2:10 b =1 3 5 7 9

5. c=[1;2;3;4;5] C=1 23. b=[1 2 3 4 1 2 3 4 5


5;6 7 8 9
2 6 7 8 9 10
10;11 12 13

2
3 14 15] 11 12 13 14 15

6. size(c) 5 1 24. b(3,3) 13

7. length(c) 5 25. b(2,5) 10

8. d=[1 2;3 4]; 2 2 26. b(1:2,3:4) 3 4

size(d) 8 9

9. length(d) 2 27. b(1,3:5) 3 4 5

10. e=magic(4) e =16 2 3 13 28. b(1:2,3) 3

5 11 10 8 8

9 7 6 12

4 14 15 1

11. f=[1 2 3]' f =1 29. b(2:end,4) 9

2 14

12. g=[1 2 3 4 1 0 0 0 0 30. a=[1 2 3;4 1 2 3


5]; 5 6;5 -10 9]
0 2 0 0 0 4 5 6
diag(g) 0 0 3 0 0 5 -10 9

0 0 0 4 0

0 0 0 0 5

13. min(g) 1 31. det(a) -102

3
14. max(g) 5 32. inv(a) -1.0294 0.4706
0.0294

0.0588 0.0588 -
0.0588

0.6373 -0.1961
0.0294

15. c=[1 2 3 4 1 2 3 4 5 33. a*a 24 -18 42


5;6 7 8 9 10] 6 7 8 9 10 54 -27 96

10 -130 36

16. min(min(c)) 1 34. a.*a 1 4 9

16 25 36

25 100 81

17. max(min(c)) 5

18. a=zeros(3,4) a =0 0 0 0

0 0 0 0

0 0 0 0

4
Exp. No. 2
2. Solving Linear and Non-linear equations using MATLAB

INPUT OUTPUT

1. Solve 3x+y=5; x+2y=5

a=[3 1;1 2]; x =1.0000

b=[5 5]'; 2.0000

x=inv(a)*b

2. Solve 5x+3y-z=15; 2x-4y+z=-4; z-y-3z=-13

a=[5 3 -1;2 -4 1;1 -1 -3]; x =2.0000

b=[15;-4;-13]; 3.0000

x=inv(a)*b 4.0000

3. Solve non-linear equation x^2-3x+2=0

solve('x^2-3*x+2') 2

4. Solve non-linear equation x^3-27=0

solve('x^3-27') 3

-3/2+3/2*i*3^(1/2)

-3/2-3/2*i*3^(1/2)

5
5. Solve the equation x^2+y^2-1; x-y-1=0

[x,y]=solve('x^2+y^2-1','x-y-1=0') x=1

y=0

-1

6. Solve the equation x^2=4

solve('x^2=4') 2

-2

6
Exp. No. 3
3. Plotting the graph of function and its derivatives

a. Plot: y=x^2
INPUT OUTPUT
x=-5:0.1:5;
y=x.^2
plot(x,y,'r')
xlabel('x-axis')
ylabel('y-axis')
title('Graph of parabola')
legend('y=x^2')

b. Plot: sin curve


INPUT OUTPUT
x=0:0.1:2*pi;
y=sin(x)
plot(x,y,'b')
xlabel('x-axis')
ylabel('y-axis')
title('sin curve')
legend('y=sin(x)')

7
c. Plot the equations: i. Y=ex ii. Y=x2 iii. Y=sin10x iv. Y=cosx
INPUT OUTPUT
x=-10:0.1:10;
y1=exp(x);
y2=x.^2;
y3=sin(10*x);
y4=cos(x);
subplot(2,2,1)
plot(x,y1)
subplot(2,2,2)
plot(x,y2)
subplot(2,2,3)
plot(x,y3)
subplot(2,2,4)
plot(x,y4)

d. Plot: y=x/(x2+1)
INPUT OUTPUT
x=linspace(0,10);
y=x./(x.^2+1)
plot(x,y,'r')
xlabel('x-axis')
ylabel('y-axis')
legend('y=x/x2+1')

8
e. Plot the function f(x)=3xsinx-2x and its derivatives both on the same figure for -2π≤x≤2π.
Plot the function with solid red line and the derivatives with black dot line.
INPUT OUTPUT
x=-2*pi:0.1:2*pi;
y=3*x.*sin(x)-2.*x
plot(x,y,'r')
xlabel('x-axis')
ylabel('y-axis')
hold on
y1=3*sin(x)+3.*x.*cos(x)-2
plot(x,y1,'--')
hold off

f. Plot: x(t)=0.4t3-2t2-5t+13; 0≤t≤7


INPUT OUTPUT
t=-7:0.01:7;
x=0.4.*t.^3-2.*t.^2-5.*t+13
plot(t,x,'r')
xlabel('x-axis')
ylabel('y-axis')
title('figure')
hold on
x1=1.2.*t.^2-4.*t-5
plot(t,x1,'--')
hold on
x2=2.4.*t-4
plot(t,x2,'b')
legend('function','velocity','acceleration')
hold off
9
Exp. No. 4
4. Plotting the 3D graph using MATLAB
i. Plot 3D graph for Z= sinx+cosy
INPUT OUTPUT

x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
mesh(x,y,z)

x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
surf(x,y,z)

x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
meshz(x,y,z)

10
x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
meshc(x,y,z)

x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
surfc(x,y,z)

x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
surfl(x,y,z)
%waterfall(x,y,z)

x=linspace(-5,5);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x)+cos(y);
waterfall(x,y,z)

11
Expt. No.: 05
5. Solving equation using Newton Raphson method
INPUT OUTPUT
A) x^3-4*x-9=0
clc; 1 3.125000
clear all; 2 2.768530
close all; 3 2.708196
a=2; 4 2.706529
n=5; 5 2.706528
f=inline('x^3-4*x-9'); %original eqn
g=inline('3*x^2-4'); %derivative
for i=1:n
a=a-f(a)/g(a);
fprintf('%d %f\n',i,a)
end

B) sin(x)+x+1=0
clc; a = 0.5000
clear all; 0.500000
close all; a = 0.5110
a=0; 0.510958
n=7; a = 0.5110
f=inline('sin(x)+x-1'); %original eqn 0.510973
g=inline('cos(x)+1'); %derivative a = 0.5110
for i=1:n 0.510973
a=a-f(a)/g(a) a = 0.5110
fprintf('%d %f\n',i,a) 0.510973
end a = 0.5110
0.510973
a = 0.5110
0.510973

12
C) x-exp(-x)
clc; 1 0.566311
clear all; 2 0.567143
close all; 3 0.567143
a=0.5;
n=3;
f=inline('x-exp(-x)'); %original eqn
g=inline('1+exp(-x)'); %derivative
for i=1:n
a=a-f(a)/g(a);
fprintf('%d %f\n',i,a)
end

D) cos(x)-x.*exp(x)
clc; 1 0.518026
clear all; 2 0.517757
close all; 3 0.517757
a=0.5;
n=3;
f=inline('cos(x)-x.*exp(x)'); %original eqn
g=inline('-sin(x)-exp(x).*(1+x)'); %derivative
for i=1:n
a=a-f(a)/g(a);
fprintf('%d %f\n',i,a)
end

13
E) x.^3-17
clc; 1 2.750000
clear all; 2 2.582645
close all; 3 2.571332
a=2; 4 2.571282
n=5; 5 2.571282
f=inline('x.^3-17'); %original eqn
g=inline('3*x.^2'); %derivative
for i=1:n
a=a-f(a)/g(a);
fprintf('%d %f\n',i,a)
end

14
Expt. No.: 06
6. Solving equation using Bisection method.
INPUT OUTPUT
A) sin(x)+x-1
clear all; 1 0.000000 1.000000 0.500000
close all;
clc; 2 0.500000 1.000000 0.750000
a=0;b=1;e=0.0001;
n1=(log(b-a)-log(e))/log(2); 3 0.500000 0.750000 0.625000
n=ceil(n1);
f=inline('sin(x)+x-1'); 4 0.500000 0.625000 0.562500
for i=1:n
c=(a+b)/2; 5 0.500000 0.562500 0.531250
fprintf('\n%d %f %f %f\n',i,a,b,c)
if f(a)*f(c)<0 6 0.500000 0.531250 0.515625
b=c;
else 7 0.500000 0.515625 0.507813
a=c;
end 8 0.507813 0.515625 0.511719
end
9 0.507813 0.511719 0.509766

10 0.509766 0.511719 0.510742

11 0.510742 0.511719 0.511230

12 0.510742 0.511230 0.510986

13 0.510742 0.510986 0.510864

14 0.510864 0.510986 0.510925


B) x^2+x-4

15
clear all; 1 0.000000 2.000000 1.000000
close all;
clc; 2 1.000000 2.000000 1.500000
a=0;b=2;e=0.01;
n1=(log(b-a)-log(e))/log(2); 3 1.500000 2.000000 1.750000
n=ceil(n1);
f=inline('x.^2+x-4'); 4 1.500000 1.750000 1.625000
for i=1:n
c=(a+b)/2; 5 1.500000 1.625000 1.562500
fprintf('\n%d %f %f %f\n',i,a,b,c)
if f(a)*f(c)<0 6 1.500000 1.562500 1.531250
b=c;
else 7 1.531250 1.562500 1.546875
a=c;
end 8 1.546875 1.562500 1.554688
end

C) cos(x)-xe(x)
clear all; 1 0.000000 1.000000 0.500000
close all;
clc; 2 0.500000 1.000000 0.750000
a=0;b=1;e=0.01;
n1=(log(b-a)-log(e))/log(2); 3 0.500000 0.750000 0.625000
n=ceil(n1);
f=inline('cos(x)-x*exp(x)'); 4 0.500000 0.625000 0.562500
for i=1:n
c=(a+b)/2; 5 0.500000 0.562500 0.531250
fprintf('\n%d %f %f %f\n',i,a,b,c)
if f(a)*f(c)<0 6 0.500000 0.531250 0.515625
b=c;
else 7 0.515625 0.531250 0.523438
a=c;

16
end
end

D) x^3-x-4
clear all; 1 1.000000 2.000000 1.500000
close all;
clc; 2 1.500000 2.000000 1.750000
a=1;b=2;e=0.01;
n1=(log(b-a)-log(e))/log(2); 3 1.750000 2.000000 1.875000
n=ceil(n1);
f=inline('x.^3-x-4'); 4 1.750000 1.875000 1.812500
for i=1:n
c=(a+b)/2; 5 1.750000 1.812500 1.781250
fprintf('\n%d %f %f %f\n',i,a,b,c)
if f(a)*f(c)<0 6 1.781250 1.812500 1.796875
b=c;
else 7 1.781250 1.796875 1.789063
a=c;
end
end

17
E) x.^2-4cos(x)
clear all; 1 1.000000 2.000000 1.500000
close all;
clc; 2 1.000000 1.500000 1.250000
a=1;b=2;e=0.01;
n1=(log(b-a)-log(e))/log(2); 3 1.000000 1.250000 1.125000
n=ceil(n1);
f=inline('x.^2-4.*cos(x)'); 4 1.125000 1.250000 1.187500
for i=1:n
c=(a+b)/2; 5 1.187500 1.250000 1.218750
fprintf('\n%d %f %f %f\n',i,a,b,c)
if f(a)*f(c)<0 6 1.187500 1.218750 1.203125
b=c;
else 7 1.187500 1.203125 1.195313
a=c;
end
end

Experiment No. 7

18
7. Evaluate using Trapezoidal rule

INPUT OUTPUT

a. f(x) = (e^x)(sinx)/(1+x^2)

clear all; f= Inline function:

close all; f(x) = (exp(x)*sin(x))/(1+x^2)

a=0; b=2; h=1/3; n=(b-a)/h; I1 = 0.4110

f=inline('(exp(x)*sin(x))/(1+x^2)') I1 = 1.2448

I0=f(a)+f(b); I1=0; I1 = 2.3885

for i=1:n-1 I1 = 3.7159

I1=I1+f(a+i*h) I1 = 5.1109

end I= 1.9276

I=(h/2)*(I0+2*I1)

b. f(x) = sin(x)

a=0; b=pi; h=pi/4; n=(b-a)/h; f= Inline function:

f=inline('sin(x)') f(x) = sin(x)

I0=f(a)+f(b); I1=0; I1 = 0.7071

for i=1:n-1 I1 = 1.7071

I1=I1+f(a+i*h) I1 = 2.4142

end I= 1.8961

I=(h/2)*(I0+2*I1)

19
c. f(x) = (1+x.^2).^-1

a=0; b=1; h=1/4; n=(b-a)/h; f= Inline function:

f=inline('(1+x.^2).^-1') f(x) = (1+x.^2).^-1

I0=f(a)+f(b); I1=0; I1 = 0.9412

for i=1:n-1 I1 = 1.7412

I1=I1+f(a+i*h) I1 = 2.3812

end I= 0.7828

I=(h/2)*(I0+2*I1)

d. f(x) = (x.^-2)

a=1; b=2; h=1/2; n=(b-a)/h; f= Inline function:

f=inline('(x.^-2)') f(x) = (x.^-2)

I0=f(a)+f(b); I1=0; I1 = 0.4444

for i=1:n-1 I= 0.5347

I1=I1+f(a+i*h)

end

I=(h/2)*(I0+2*I1)

Experiment No. 8
20
8. Evaluate using Simpson’s 1/3 Rule
INPUT OUTPUT

a. f(x) = (1+x) ^-1

f = @(x)(1+x).^-1; 0.6933
a = 0;
b = 1;
n = 4;
h = (b - a) / n;
x = a:h:b;
y = f(x);
result = (h / 3) * (y(1) + 4 * sum(y(2:2:end-
1)) + 2 * sum(y(3:2:end-2)) + y(end));
disp(result);

b. f(x) = 3x ^2

f = @(x)3*x.^2; 63
a = 1;
b = 4;
n = 6;
h = (b - a) / n;
x = a:h:b;
y = f(x);
result = (h / 3) * (y(1) + 4 * sum(y(2:2:end-
1)) + 2 * sum(y(3:2:end-2)) + y(end));
disp(result);

c. f(x) = sin(x)

21
f = @(x)sin(x); 2.0009
a = 0;
b = pi;
n = 6;
h = (b - a) / n;
x = a:h:b;
y = f(x);
result = (h / 3) * (y(1) + 4 * sum(y(2:2:end-
1)) + 2 * sum(y(3:2:end-2)) + y(end));
disp(result);

d. f(x) = cos(x)

f = @(x)cos(x); 1.0000
a = 0;
b = pi/2;
n = 6;
h = (b - a) / n;
x = a:h:b;
y = f(x);
result = (h / 3) * (y(1) + 4 * sum(y(2:2:end-
1)) + 2 * sum(y(3:2:end-2)) + y(end));
disp(result);

Experiment No. 9

22
9. Evaluate using Euler’s method.

INPUT OUTPUT

a. Given that dy/dt+20y=7e^(-0.5t), y(0)=5, compute y(0.2) by taking h=0.1.

f=@(t,y)-20*y+7*exp(-0.5*t) f= @(t,y)-20*y+7*exp(-0.5*t)

t0=0; y(1.00)=-4.3000

tn=0.2; y(1.00)=4.7246

h=0.1;

n=(tn-t0)/h;

t(1)=0;

y(1)=5;

for i=1:n

y(i+1)=y(i)+h*f(t(i),y(i));

t(i+1)=t0+1;

fprintf('y(%.2f)=%.4f\n',t(i+1),y(i+1))

end

b. Approximate the solution of the initial value problem y’=2x+y, y(0)=1 with step size of
0.1. Approximate the value of y(0.4).

23
f=@(x,y)+2*x+y f= @(x,y)+2*x+y

t0=0; y(1.00)=1.1000

tn=0.4; y(1.00)=1.4100

h=0.1; y(1.00)=1.7510

n=(tn-t0)/h; y(1.00)=2.1261

t(1)=0;

y(1)=1;

for i=1:n

y(i+1)=y(i)+h*f(t(i),y(i));

t(i+1)=t0+1;

fprintf('y(%.2f)=%.4f\n',t(i+1),y(i+1))

end

c. Find y(2.2) from the equation dy/dx=-2xy^2 with y(0)=1 with h=0.2 in the interval
[0,1].

24
f=@(x,y)-2*x*(y.^2) f= @(x,y)-2*x*(y.^2)

t0=0; y(1.00)=1.0000

tn=2.2; y(1.00)=0.6000

h=0.2; y(1.00)=0.4560

n=(tn-t0)/h; y(1.00)=0.3728

t(1)=0; y(1.00)=0.3172

y(1)=1; y(1.00)=0.2770

for i=1:n y(1.00)=0.2463

y(i+1)=y(i)+h*f(t(i),y(i)); y(1.00)=0.2220

t(i+1)=t0+1; y(1.00)=0.2023

fprintf('y(%.2f)=%.4f\n',t(i+1),y(i+1)) y(1.00)=0.1859

end y(1.00)=0.1721

25

You might also like