How should my set my obtained answers into a table form?
29 views (last 30 days)
Show older comments
Hi! May I kindly ask for your help I have been working on Jacobi Method since yesterday and I haven’t figured it out yet I don’t have someone to ask. I would appreciate your help…
I wanted to show the results of my codes like this.....
example values only....
Iteration x1 x2 x3
1 3.00000 4.00000 4.60000
2 4.00000 4.60000 4.27076
3 4.60000 4.27076 4.30295
4 4.27076 4.30295 4.30697
5 4.30295 4.30697 4.30691
The roots are the following:
x1 = 1
x2 = 2
x3 =-1
Iteration = 9
Inputs are....
Enter the number of equations, n:
3
Enter absolute errror, Ea:
0.0001
Enter coefficient (Diagaonally Dominant), Matrix A:
[4 -1 -1;-2 6 1;-1 1 7]
Enter constants, Matrix B:
[3;9;-6]
Enter the initial guess, x0,x1,x2:
[0;0;0]
CODE
format short
Eqn=input('Enter the number of equations, n: ');
Error=input('Enter absolute errror, Ea: ')
A=input('Enter coefficients (DIAGONALLY DOMINANT!), Matrix A: ');
B=input('Enter constants, Matrix B: ');
C=input('Enter the initial guess: x0,x1,x2: ');
D=[A B]
N=length(B);
x=zeros(N,1);
for j=1:100
for i=1:N
x(i) = round((B(i)/A(i,i)) - (A(i,[1:i-1,i+1:N])*C([1:i-1,i+1:N]))/A(i,i),4);
end
fprintf('Iteration no %d \n',j)
x
if abs(x-C)<=Error
break
end
C = x;
end
Accepted Answer
Star Strider
on 27 Jun 2022
Copying from my answer to your previous post:
% Secant Method in MATLAB
% a=input('Enter function:','s');
% f=inline(a)
a = 'x^3 - 4*x^2 + x - 10'
f = str2func(['@(x)' a])
% x0 = input ('Enter the first initial guess: ')
% x1 = input ('Enter the second initial guess: ')
% Ea = input ('Enter absolute error: ')
x0 = 3;
x1 = 4;
Ea = 0.0001;
iteration=0;
% disp('Iteration x0 x1 x2 f(x0) f(x1) f(x2) Ea')
for i=1:1000
x2 = x0 - ((f(x0))*((x1-x0)/(f(x1)-f(x0))));
Error = round(abs(x2-x1),4);
% fprintf(['%i %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f\n'],i,x0,x1,x2,f(x0),f(x1),f(x2),Error)
itervec(i,:) = [i,x0,x1,x2,f(x0),f(x1),f(x2),Error];
iteration=iteration+1;
if (Error<=Ea)
Root=x2
Error=Ea;
Iteration=iteration
break
end
x0=x1;
x1=x2;
end
Results = array2table(itervec, 'VariableNames',{'Iteration', 'x0', 'x1', 'x2', 'f(x0)', 'f(x1)', 'f(x2)', 'Ea'})
Creating the table requires saving the intermediate results to a vector and then using array2table to load them into a table. (I am not certain what to do with the ‘Root’ variable, since it was not part of the original output array.)
.
4 Comments
Star Strider
on 27 Jun 2022
As always, my pleasure!
The best way to return the favour is to use your growing knowledge to help others become proficient with MATLAB!
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!