0% found this document useful (0 votes)
29 views6 pages

Assignment 4

The document contains 4 questions regarding solving systems of linear equations using MATLAB. Question 1 uses Gaussian elimination to solve a system of equations. Question 2 also uses Gaussian elimination to solve another system. Question 3 plots data points and fits linear and quadratic curves. Question 4 uses polynomial interpolation to find values of a function at given points.

Uploaded by

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

Assignment 4

The document contains 4 questions regarding solving systems of linear equations using MATLAB. Question 1 uses Gaussian elimination to solve a system of equations. Question 2 also uses Gaussian elimination to solve another system. Question 3 plots data points and fits linear and quadratic curves. Question 4 uses polynomial interpolation to find values of a function at given points.

Uploaded by

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

Qn.

1
Matlab code:
A=[1 -2 -6 12;2 4 12 -17;1 -4 -12 22];
[x,y]=size(A) ;
t=0 ;
D=zeros(x,y) ;
AA=zeros(x,y) ;
while(numel(A)>=6)
for i=1
[m,n]=size(A) ;
a1=A(i:m,i) ;
Ha1=zeros(size(a1)) ;
a=sqrt(a1'*a1) ;
if a1(1) > 0
a=-a ;
else
a=a ;
end
w11=a1(1)-a ;
f1=sqrt(-2*w11*a) ;
Ha1(1)=a ;
v=(a1-Ha1)/f1;
AA(i:m,i)=Ha1 ;
for j=1:n-1
a2=A(i:m,j+1) ;
f2=2*v'*a2 ;
Ha2=a2-f2*v ;
AA(i:m,j+1)=Ha2;
end
t=t+1 ;
A=AA([t+1:m],[t+1:n]) ;
D(t:x,t:y)=AA ;
end
AA=A ;
end
P=D(1:x,1:y-1) ;
Q=D([1:x],y) ;
Xvalues=P\Q ;
disp("x values =");
disp(Xvalues);

Result:
x values =
1.0e+13 *
0.0000
7.7327
-2.5776

By gauss jakobi method


Matlab code:
f3=@(x,y) (x-(2*y)-12)/6 ;
f2=@(z,x) (-17-(2*x)-(12*z))/4 ;
f1=@(y,z) 22+(4*y)+(12*z) ;
x=1 ;
y=1 ;
z=1 ;
newx=f1(y,z) ;
newy=f2(z,x) ;
newz=f3(x,y) ;
n=input('enter number of iterations') ;
while(n>0)
x=f1(newy,newz) ;
y=f2(newz,newx) ;
z=f3(newx,newy) ;
newx=f1(y,z) ;
newy=f2(z,x) ;
newz=f3(x,y) ;
n=n-1 ;
end
jacval=[newx ;newy; newz] ;
disp("xvalues= ");
disp(jacval);

Result:
enter number of iterations1000
xvalues=
38.0000
242.2500
-85.5000

enter number of iterations10000


xvalues=
1.0e+03 *

0.0380
2.4922
-0.8355
enter number of iterations25000
xvalues=
1.0e+03 *

0.0380
6.2423
-2.0855

Qn.2
Matlab code:
A=[400 -201 200;-800 401 -200];
[x,y]=size(A) ;
t=0 ;
D=zeros(x,y) ;
AA=zeros(x,y) ;
while(numel(A)>=6)
for i=1
[m,n]=size(A) ;
a1=A(i:m,i) ;
Ha1=zeros(size(a1)) ;
a=sqrt(a1'*a1) ;
if a1(1) > 0
a=-a ;
else
a=a ;
end
w11=a1(1)-a ;
f1=sqrt(-2*w11*a) ;
Ha1(1)=a ;
v=(a1-Ha1)/f1;
AA(i:m,i)=Ha1 ;
for j=1:n-1
a2=A(i:m,j+1) ;
f2=2*v'*a2 ;
Ha2=a2-f2*v ;
AA(i:m,j+1)=Ha2;
end
t=t+1 ;
A=AA([t+1:m],[t+1:n]) ;
D(t:x,t:y)=AA ;
end
AA=A ;
end
P=D(1:x,1:y-1) ;
Q=D([1:x],y) ;
Xvalues=P\Q ;
disp("x values =");
disp(Xvalues);
Result:
x values =
-100.0000
-200.0000

By gauss jakobi method


Matlab code:
f2=@(x) (400*x-200)/201 ;
f1=@(y) (401*y+200)/800 ;
x=1 ;
y=1 ;
newx=f1(y) ;
newy=f2(x) ;
n=input('enter number of iterations') ;
while(n>0)
x=f1(newy) ;
y=f2(newx) ;
newx=f1(y) ;
newy=f2(x) ;
n=n-1 ;
end
jacval=[newx newy] ;
disp("xvalues= ");
disp(jacval);

Result:
enter number of iterations1000
xvalues=
-91.6522 -183.3465
enter number of iterations2500
xvalues=
-99.8009 -199.6028
enter number of iterations5000
xvalues=
-99.9996 -199.9992
enter number of iterations7500
xvalues=
-100.0000 -200.0000

Qn.3
Matlab code:
x=[1, 2.5, 3.5, 4, 1.1, 1.8, 2.2, 3.7];
y=[6.008, 15.722, 27.130, 33.772, 5.257, 9.549, 11.098, 28.828];
plot(x,y,'o')
strline=polyfit(x,y,1);
strlinx=linspace(min(x),max(x),100);
strliny=polyval(strline,strlinx);
hold on
plot(strlinx,strliny,"r",'DisplayName',"linear")
quadline=polyfit(x,y,2);
quadlinx=strlinx;
quadliny=polyval(quadline,quadlinx);
hold on
plot(quadlinx,quadliny,"g",'DisplayName',"quadratic")

Result:

Qn.4
Matlab code:
x = [-2 1 4 -1 3 -4];
y = [-1 2 59 4 24 -53];
xvalues = [1.2, -3.5];
n = length(x);
F(:,1) = y';
for j = 2:n
for i = j:n
F(i,j) = (F(i,j-1) - F(i-1,j-1)) / (x(i) - x(i-j+1));
end
end
yvalues = zeros(size(xvalues));
for k = 1:length(xvalues)
soln = F(1,1);
for i = 2:n
b = F(i,i);
for j = 1:i-1
b = b * (xvalues(k) - x(j));
end
soln = soln + b;
end
yvalues(k) = soln;
end
fprintf('At x = %.1f, y = %.4f\n', [xvalues; yvalues])

Result:
At x = 1.2, y = 2.3280
At x = -3.5, y = -32.8750

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy