I have just started learning how to use MATLAB&I have been tryn to solve this problem now for a while but keep coming up with errors. We were asked to write a function in MATLAB for Jacobi Method&the function I wrote is as follows:
function x = jacobi(A,b,y,N)
% This function applies N iterations of Jacobi's method to the system of linear equations Ax=b
n = length(y);
for k=1:N
for i=1:n
sum=b(i);
for j=1:i-1
sum = sum - A(i,j)*y(j);
end
for j = i+1:n
sum = sum - A(i,j)*y(j);
end
x(i) = sum/A(i,i);
end
y = x';
end
Today in class we were asked to perform 5 iterations with A=[4,1,-1;-1,3,1;2,2,5], b=[5;-4;1]&an inital estimate of x(0)= [4,2,1]^T. I wrote the following script file to do this but I keep coming up the error:
A=input('Enter a matrix ');
b=input('Enter a vector ');
x=input('Enter initial estimate ');
n=input('Enter number of iterations ');
for i=1:n
x=jacobi(A,b,y);
disp(x)
end
Error:
??? Input argument "N" is undefined.
Error in ==>jacobi at 5
Tags: