I need help formulating this script into a function.

조회 수: 6 (최근 30일)
James Elmore
James Elmore 2012년 2월 9일
편집: Syeda 2013년 10월 8일
I'm new to MatLab and I'm not certain why I'm getting certain errors that I am.
I'm getting a Matrix dimension error and I'm not certain why... Any ideas? The error happens on line 61, where the normalized error is being calculated. I'll put it in bold.
function [] = JacobIterMeth(n, alpha, N);
%%Jacobi Iterative Technique
% Solve A*x=b, given an initial guess Xo;
%%Input Data
% 'n' is our chosen matrix size, 'alpha' is our given value of ... alpha, A
% is our tridiagonal matrix as specified, and the b is the b. The 'tol'
% variable is the tolerance in order to accept an approximation as being
% of adequate accuracy, x0 is our inital guess and N is the number of
% iterations. 'errorJIM(k)' is the assosiated error with that last
% iteration of the Jacobi MethodThe 'sol' variable remains false until an
% adeguate estimation is found, it acts as a limiter on the while loop in
% the next section.
A = eye(n) + (-alpha)*diag(ones(1,n-1), -1) + (1-alpha)*diag(ones(1,n-1), 1);
b = zeros(1,n);
b(1) = 1;
tol=0.000001;
errorJIM = zeros(1,n);
False=0;
True=1;
sol=False;
x0 = zeros(1,n);
k=1;
ex = zeros(2,n);
while k<N && sol==False
%This runs through each component of x and sums all the other
%components before implementing the Jacobian Iterave Method Equation.
for i=1:1:n
sumj=0;
for j=1:n
if i~=j
sumj=sumj+A(i,j)*x0(j);
end
end
x(i)=(-sumj+b(i))/A(i,i);
if(k<=2)
ex(k,i) = x(i);
else
end
end
%This checks to see if an accurate estimate has been made or if the
%process should be repeated.
*errorJIM(k) = norm(x' - x0, inf)/(norm(ex(2,:)' - ex(1,:)', inf));*
if errorJIM(k)<=tol
sol=True;
else
k=k+1;
x0=x';
end
end
%Judges the results of the code. And plots a figure of the results.
if errorJIM(end)>tol
disp('Maximum Number of Iterations Exceeded');
else
disp('The procedure was successful, a result of')
disp(x)
disp('was found.')
end
figure
plot(errorJIM, '-*');
xlabel('Iteration No.');
ylabel('Error (norm inf)');
  댓글 수: 2
Jan
Jan 2012년 2월 9일
Which is the line 61? The line in the starts?
You have posted a function already, therefore I do not understand the connection between the subject and the body of your question.
Another tip: Whenever you ask a question about an error here, post the complete error message.
James Elmore
James Elmore 2012년 2월 9일
Here is the full error code:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> JacobIterMeth at 61
errorJIM(k) = norm(x' - x0, inf)/(norm(ex(2,:)' - ex(1,:)',
inf));
I've been slowly trying to transform it into a function but it still doesn't seem to want to work...

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2012년 2월 9일
You can ask the debugger to support you:
dbstop if error
Then the program stops when the error occurs and you can check if:
norm(x' - x0, inf) / (norm(ex(2,:)' - ex(1,:)', inf))
is a scalar, such that it fits into the scalar errorJIM(k).
  댓글 수: 2
James Elmore
James Elmore 2012년 2월 9일
well Norm takes a vector or a matrix and gives you a scalar. So that expression should be a scalar... I was thinking the hang up was with the "ex(2,:) - ex(1,:)" piece?
Jan
Jan 2012년 2월 11일
If you use the debugger, you can simply try it. Enable it as shown above, run your code until it stops and then try each single part of the line in the command window. Then you can find out, what is happending exactly.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by