I need help formulating this script into a function.
6 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!