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

Q1. Modify The MATLAB Code For Jacobi's Iterative ...

Uploaded by

Malik Asad
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)
110 views

Q1. Modify The MATLAB Code For Jacobi's Iterative ...

Uploaded by

Malik Asad
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

Alert when this question is answered

× Download the Chegg Study App GE T


★★★★★ (80K+)

  Textbook Solutions Expert Q&A Practice 

home / study / math / advanced math / advanced math questions and answers / q1. modify the matlab...
Find solutions for your homework

Question: Q1. Modify the MATLAB code for Jacobi’s iterative


scheme (provided and explained in recorded lect...

Q1. Modify the MATLAB code for Jacobi’s iterative scheme (provided and explained in recorded lectures) to
be used for Gauss Seidel iterative formula. Illustrate this with an example.

Q2. Write a MATLAB program to construct and evaluate Lagrange interpolating polynomial for n+1 data
points. Illustrate this with an example.

Expert Answer

Anonymous
answered this

n = input('enter the number of equations : ')

disp('enter the augmented matrix element-wise')


for i = 1:n
for j = 1:n+1
fprintf('enter (%d,%d)th element :',i,j)
A(i,j) = input('') ;
end
end

disp('The augmented matrix is : ') ;


A
b = A(:,n+1) ;

A(:,n+1) =[] ;

disp('now entet the starting vector of the iteration element-wise :')


x = zeros(n,1) ;

for i = 1:n
fprintf('enter (%d)th element :',i)
x(i) = input('') ;
end

normVal=Inf;

%Tolerence for method


tol=1e-5; itr=0;

while normVal>tol
x_old=x;

for i=1:n

sigma=0;

for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end

for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end

x(i)=(1/A(i,i))*(b(i)-sigma);
end

itr=itr+1;
normVal=norm(x_old-x);
end
disp('The solution vector found by Gauss-Seidel iteration becomes :') ;
x

>>
x = [ 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 ] ;
y = [ 0 0.24746646 0.88137359 1.55015796 2.09471255 2.53206806 2.89344399 3.20033494 3.46671104
3.70191108 3.9124227 ] ;

x_test = linspace(0,5,100) ;

y_lagrange = [] ;

n = length(x) ;
N = length(x_test) ;

for i = 1:N
y_lagrange(i) = 0 ;
for j = 1:n
P = y(j) ;
for k = 1:n
if (j ~= k)
P = P * (x_test(i) - x(k))/(x(j)-x(k)) ;
end
end
y_lagrange(i) = y_lagrange(i) + P ;
end
end

y_spline = spline(x,y,x_test) ;
err_lagrange = y - interp1(x,y,x) ;
err_spline = y - spline(x,y,x) ;

disp("total error in lagrange interpolation : ")


disp(mean(err_lagrange.^2)) ;

disp("total error in cubic spline interpolation : ")


disp(mean(err_spline.^2)) ;

subplot(2,1,1) ;
scatter(x , y ,"*","b") ;
hold on ;

plot(x_test , y_lagrange , "r") ;

legend("data points" , "lagrange") ;


hold off;

subplot(2,1,2) ;
scatter(x , y ,"*","b") ;
hold on ;

plot(x_test , y_spline , "g") ;

legend("data points" , "cubic spline") ;


hold off ;

total error in lagrange interpolation :


0

total error in cubic spline interpolation :


0

>>
0 Comments

Was this answer helpful? 0

0
Up next for you in Advanced Math

consider the 4th order


Runge-Kutta method

See more questions


for subjects you study

See answer

COMPANY
LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

You might also like