Matlab I N Section21
Matlab I N Section21
Matlab I N Section21
Source Code:
n=1;
while n<=10
f(n)=cos(n*pi)+sin(n*pi/2);
n=n+1;
end
disp(f)
ii) p=[1, 1, 1, 2]
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. 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
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);
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
𝒅𝒚
Q2. Solve = 𝒙 𝟑 𝒚𝟐
𝒅𝒙
𝒅𝒚
Q3. Solve = 𝒔𝒊𝒏𝒙 𝒄𝒐𝒔𝒚 subjected to y(0)=0
𝒅𝒙
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