Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Matlab I N Section21

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

PRACTICAL 1 – LOOPING STRUCTUES

Q1. Evaluate the value of 2! to 6! 4!= 4x3x2x1=24


Source Code:
fact=1;
for i=2:6
fact=fact*i; %i=2fact=1x2=2i=3fact=2x3=6i=4,fact=6x4=24
fprintf(‘%d!= %d \n’,i,fact)
end
Q2. Sum of the series ∑𝟔𝒏=𝟏 𝟐𝒏 21+22+23……..+26
+++Source Code:
sum=0;
for n=1:6
sum=sum+2^n; %0+21sum=2+22=6sum=6+23
end
disp(sum)
fprintf(‘%d is the sum 0f 2n from n=1 to 6’,sum)

Q3. Evaluate ∏𝟏𝟎


𝒏=𝟏(𝒏 + 𝟐) % multiplicative sum….(n+2)(n+2)……..(n+2)
Source Code:
n=10;
f=n+2;
while n>1
n=n-1;
f=f*(n+2); %msumf=n+2=10+2=12 f=12 f=12*(9+2)*(8+2)*(7+2)……(1+2)
end
disp(f)

Q4. Evaluate 𝐜𝐨𝐬 𝒏𝝅 for n=1 to 10


Source Code:
n=1;
while n<=10
f(n)=cos(n*pi); 𝐜𝐨𝐬 𝒏𝝅= (-1)n
n=n+1;
end
disp(f)
𝒏𝝅
Q5. Evaluate 𝐜𝐨𝐬 𝒏𝝅 + 𝐬𝐢𝐧 for n=1 , 2 , …. 10
𝟐

Source Code:
n=1;
while n<=10
f(n)=cos(n*pi)+sin(n*pi/2);
n=n+1;
end
disp(f)

PRACTICAL 2 – EVALUATING ROOTS OF


POLYNOMIALS
Q. Estimate the roots of the following polynomials.
i) 𝑥 4 + 16𝑥 3 − 7𝑥 2 − 3𝑥 + 9 = 0
p= [1,16, -7 ,-3 ,9]
roots(p)
3 2
ii) 𝑥 +𝑥 +𝑥+2=0
p= [ 1 ,1,1,2]
iii) 𝑥 3 − 5𝑥 + 4 = 0
p= [1,0,-5, 4]
iv) 𝑥7 + 𝑥5 + 𝑥 + 2 = 0
p= [1, 0, 1, 0, 0, 0 ,1, 2]
V) 𝑥 3 − 2𝑥 + 5 = 0
P= [ 1 ,0 ,-2 , 5]
Source Code:
i) p=[1, 16, -7, -3, 9]
roots(p)

ii) p=[1, 1, 1, 2]
roots(p)

iii) p=[1, 0, -5, 4]


roots(p)

iv) p=[1, 0, 1, 0, 0, 0, 1, 2]
roots(p)
PRACTICAL 3 – INTERPOLATION
Q1. Interpolate at 0.5 , 1.5 , 2.5 and 3.5 for the data.

x 0 1 2 3 4
y 7 10 14 17 22
And plot x, y, xq, yq
Source Code:
x=[0, 1, 2, 3, 4]
y=[7, 10, 14, 17, 22]
xq=[0.5, 1.5, 2.5, 3.5]
yq=interp1(x, y, xq, ‘pchip’)
plot(x,y,xq, yq)

OUTPUT:
x= 0 1 2 3 4
y = 7 10 14 17 22
xq = 0.5000 1.5000 2.5000 3.5000
yq = 8.3839 12.0000 15.4598 19.2188

Q2. Interpolate at 2.5 and 3.5 for the data.


x 1 2 3 4 5
y 5.5 6.6 7.8 8.9 10
Source Code:
x=[1, 2, 3, 4, 5]
y=[5.5, 6.6, 7.8, 8.9, 10]
xq=[2.5, 3.5]
yq=interp1(x, y, xq, ‘pchip’)
OUTPUT:
x= 1 2 3 4 5
y = 5.5000 6.6000 7.8000 8.9000 10.0000
xq = 2.5000 3.5000
yq = 7.2000 8.3560
PRACTICAL 4 – FITTING POLYNOMIALS
Q1. Fit a polynomial of degree 2 (form is 𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄),
3 (form is 𝒂𝒙𝟑 + 𝒃𝒙𝟐 + 𝒄𝒙 + 𝒅) and 4 (form is 𝒂𝒙𝟒 + 𝒃𝒙𝟑 + 𝒄𝒙𝟐 + 𝒅𝒙 + 𝒆)
for the data.
x 1 2 3 4 5
y 12 17 27 39 46
Source Code:
x=[1, 2, 3, 4, 5];
y=[12, 17, 27, 39, 46];
p1=polyfit(x, y, 2) %{note: for nth degree syntax: p=polyfit(x, y, n) }
p2= polyfit(x, y, 3)
p3= polyfit(x, y, 4)
x2=1:0.1:10;
y1=polyval(p1, x2)
y2=polyval(p2, x2)
y3=polyval(p3, x2)
plot(x, y, ‘o’,x1,y1,’-‘,x2,y2,x3,y3)

Q2. Fit a polynomial of degree 3 and plot the calculated values for the data
x 2 4 6 8 10
y 21 32 44 57 63
Source Code:
x=[2, 4, 6, 8, 10]
y=[21, 32, 44, 57, 63]
p=polyfit(x, y, 3)
x2=1:0.1:10;
y2=polyval(p, x2)
plot(x, y, ‘o’,x2,y2)

PRACTICAL 5 – LU DECOMPOSITION
Q. Decompose the following matrices into L and U
𝟏 𝟏 𝟏 𝟐 𝟎 𝟒 3 2 7  2 3 1  2 1 4 
[𝟐 𝟏 𝟑] b) [𝟒 𝟑 𝟏] c)  2 3 1  d) 1 2 3  c) 8 −3 2 
 
𝟑 𝟐 𝟒 𝟏 𝟐 𝟐  3 4 1   3 1 2   4 11 −1
( note : L is permuted lower triangular matrix , U is upper triangular matrix )

Code: A=[3,2,7;2,3,1;3,4,1]
[L, U]=lu(A)
0.3333 1.0000 0
0.6667 -1.0000 1
1.0000 0 0
3.0000 2.0000 4.0000

0 0.3333 -0.3333
0 0 0
0.0000

PRACTICAL 6 -NEWTON RAPHSON METHOD


Q1. Find the real root of the equation ⅇ𝐱 − 𝟓𝒙 by Newton Raphson Method.
f=@(x) exp(x)-5*x;
df=@(x) exp(x)-5;
x0=0
e=0.0001 %0.6042 0.6042
n=20; %for i=.1:.1:10forstart:step:endvalue
f ( x0 )
if df(x0)~=0 % x1 = x0 −
f ( x0 )
for i=1:n
x1=x0-(f(x0)/df(x0));
fprintf('x%d = %.5f \n',i,x1)
if abs(x1-x0)<e
break
end
if df(x1)==0
disp('Newton-Raphson failed');
end
x0=x1;
end
end
OUTPUT:
X0 = 0
x1 = 0.2500
x2 = 0.2592
x3 = 0.2592

Q2. Find the real root of the equation 𝒙𝒍𝒐𝒈𝟏𝟎 𝒙 − 𝟏. 𝟐 by Newton Raphson Method lies
near 2.
f=@(x) x*log10(x)-1.2;
df=@(x) 0.4343+log10(x);
x0=2
e=0.0001
n=20;
if df(x0)~=0
for i=1:n
x1=x0-(f(x0)/df(x0));
fprintf('x%d = %.4f \n',i,x1)
if abs(x1-x0)<e
break
end
if df(x1)==0
disp('Newton-Raphson failed');
end
x0=x1;
end

end

OUTPUT:
e = 1.0000e-04
x0 = 2
x1 = 2.8132
x2 = 2.7411
x3 = 2.7406
x4 = 2.7406

OR
f=input('Enter the function:');
df=input('Enter the derivative of the given function:');
e=input('Enter the tolerance:');
x0=input('Enter the initial guess:');
n=input('Enter the no.of iteration:');
if df(x0)~=0
for i=1:n
x1=x0-(f(x0)/df(x0));
fprintf('x%d = %.4f \n',i,x1)
if abs(x1-x0)<e
break
end
if df(x1)==0
disp('Newton-Raphson failed');
end
x0=x1;
end

end
OUTPUT:
Enter the function:@(x) exp(x)-5*x;
Enter the derivative of the given function:@(x) exp(x)-5;
Enter the tolerance:0.0001
Enter the initial guess:0
Enter the no.of iteration:20
x1 = 0.2500
x2 = 0.2592
x3 = 0.2592
PRACTICAL 7 – LAGRANGE INTERPOLATION

Q1. For the data find f(5) given f(1)=11, f(3)=23, f(6)=34, f(8)=44
Code: x=[1, 3, 6, 8]
y=[11, 23, 34, 44]
a=5;
sum=0;
for i=1:length(x)
u=1;
l=1;
for j=1:length(x)
if j~=i
u=u*(a-x(j));
l=l*(x(i)-x(j));
end
end
sum=sum+(u/l)*y(i);
end
fprintf('The value of y= %.5f for x= %.5f by Lagrange’s interpolation
method',sum, a);

Q2. For the data find f(12.5) given x= 12 13 14 16


y= 5 6 9 11
x=[12 13 14 16];
y=[5 6 9 11];
a=12.5;
sum=0;
for i=1:length(x)
u=1;
l=1;
for j=1:length(x)
if j~=i
u=u*(a-x(j));
l=l*(x(i)-x(j));
end
end
sum=sum+(u/l)*y(i);
end
fprintf('The value of y= %.5f for x= %.5f by Lagranges interpolation
method',sum, a);
OUTPUT:
The value of y= 5.09375 for x= 1.250000e+01 by Lagrange’s interpolation
method

x=4 5 7 10 11 13 a=11.5
Y=5 6 9 4 3 8
x=[4 5 7 10 11 13];
y=[5 6 9 4 3 8];
a=11.5;
sum=0;
for i=1:length(x)
u=1;
l=1;
for j=1:length(x)
if j~=i
u=u*(a-x(j));
l=l*(x(i)-x(j));
end
end
sum=sum+(u/l)*y(i);
end
fprintf('The value of y= %.5f for x= %.5f by Lagranges interpolation
method',sum, a);
OUTPUT:
The value of y= 3.29046 for x= 11.50000 by Lagrange’s interpolation method

PRACTICAL 8 – SOLUTION OF 1ST ORDER ODE


𝒅𝒚
Q1. Solve = 𝒆𝒚 𝒙 𝟐
𝒅𝒙
Code: syms y(x)
ode=diff(y,x)==(exp(y))*(x^2)
ysol(x)=dsolve(ode)

𝒅𝒚
Q2. Solve = 𝒙 𝟑 𝒚𝟐
𝒅𝒙

Code: syms y(x)


ode=diff(y,x)==(x^3)*(y^2)
ysol(x)=dsolve(ode)

𝒅𝒚
Q3. Solve = 𝒔𝒊𝒏𝒙 𝒄𝒐𝒔𝒚 subjected to y(0)=0
𝒅𝒙

Code: syms y(x)


ode=diff(y,x)==sin(x)*cos(y)
cond=y(0)==0;
ysol(x)=dsolve(ode, cond)
PRACTICAL 9 – SOLUTION OF 2nd ORDER ODE
𝒅𝟐 𝒚
Q1. Solve + 𝒚 = 𝒄𝒐𝒔𝒙
𝒅𝒙𝟐
𝒅𝟐 𝒚
= 𝒄𝒐𝒔𝒙 − 𝒚
𝒅𝒙𝟐
Source Code:
syms y(x)
ode=diff(y, x, 2)==cos(x)-y
ysol=dsolve(ode)
𝒅𝟐 𝒚 𝒅𝒚
Q2. Solve − 𝟑 𝒅𝒙 + 𝒚 = 𝟎, 𝒚(𝟎) = 𝟏 & 𝒚𝟏 (𝟎) = 𝟎
𝒅𝒙𝟐
𝒅𝟐 𝒚 𝒅𝒚
𝟐
=𝟑 −𝒚
𝒅𝒙 𝒅𝒙
Source Code:
syms y(x)
Dy=diff(y, x) %diff(y,x,order)
ode=diff(y, x, 2)==3*diff(y,x)-y
cond1=y(0)==1
cond2=Dy(0)==0
conds=[cond1, cond2]
ysol=dsolve(ode,conds)
ysol=simplify(ysol)
PRACTICAL 10 – GAUSS -SEIDEL METHOD

X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
Q1. Solve the system of linear equations.
𝒙+𝒚+𝒛+𝒘=𝟔;
𝒙 + 𝟐𝒚 + 𝟑𝒛 + 𝟒𝒘 = 𝟏𝟒
𝒙 + 𝟒𝒚 + 𝟗𝒛 + 𝟗𝒘 = 𝟑𝟔
𝒙 + 𝟐𝒚 + 𝟐𝒛 + 𝟑𝒘 = 𝟓𝟓

Source Code:
A=input('Enter a coefficient matrix A:\n');
B=input('Enter a constant column matrix B:\n');
P=input('Enter initial guess vector X(0):\n');
n=input('Enter no of iterations :\n');
e=input('Enter your error tolerance, no of decimal places of
accuracy :\n');
N=length(B);
X=zeros(N,1);

for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
P(i)=X(i)
end
fprintf('Iteratiin no %d \n',j)
X
End

Output:
>> gaussseidl
Enter a coefficient matrix A:
[1,1,1,1; 1,2,3,4; 1,4,9,9; 1,2,2,3]
Enter a constant column matrix B:
[6;14;36;55]
Enter initial guess vector X(0):
[0;0;0;0]
Enter no of iterations :
10
Enter your error tolerance, no of decimal places of accuracy :
0.0001

P=

1.0e+03 *

0.3575
-1.1446
-0.0350
0.6856

Iteratiin no 10

X=

1.0e+03 *

0.3575
-1.1446
-0.0350
0.6856

Q2. Solve the system of linear equations.


𝟒𝒙 + 𝟐𝒚 + 𝒛 = 𝟏𝟒
𝒙 + 𝟓𝒚 − 𝒛 = 𝟏𝟎
𝒙 + 𝒚 + 𝟖𝒛 = 𝟐𝟎

Q3. Solve the system of linear equations.


𝟐𝟖𝒙 + 𝟒𝒚 − 𝒛 = 𝟑𝟐
𝒙 + 𝟑𝒚 + 𝟏𝟎𝒛 = 𝟐𝟒
𝟐𝒙 + 𝟏𝟕𝒚 + 𝟒𝒛 = 𝟑𝟓

Q4. Solve the system of linear equations.


𝟏𝟎𝒙 + 𝒚 + 𝒛 = 𝟏𝟐
𝟐 𝒙 + 𝟏𝟎𝒚 − 𝒛 = 𝟏𝟑
𝟐 𝒙 + 𝟐𝒚 + 𝟏𝟎𝒛 = 𝟏𝟒

Q5. Solve the system of linear equations.


𝟓𝒙 + 𝟐𝒚 + 𝒛 = 𝟏𝟐
𝒙 + 𝟒𝒚 + 𝟐𝒛 = 𝟏𝟓
𝒙 + 𝟐𝒚 + 𝟓𝒛 = 𝟐𝟎

You might also like