Effacer les filtres
Effacer les filtres

How should my set my obtained answers into a table form?

2 vues (au cours des 30 derniers jours)
Christoppe
Christoppe le 27 Juin 2022
Commenté : Star Strider le 27 Juin 2022
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
  1 commentaire
Christoppe
Christoppe le 27 Juin 2022
update: I already figured how to do The roots are the following:
x1 = 1
x2 = 2
x3 =-1
Iteration = 9

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 27 Juin 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'
a = 'x^3 - 4*x^2 + x - 10'
f = str2func(['@(x)' a])
f = function_handle with value:
@(x)x^3-4*x^2+x-10
% 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
Root = 4.3069
Iteration = 5
Results = array2table(itervec, 'VariableNames',{'Iteration', 'x0', 'x1', 'x2', 'f(x0)', 'f(x1)', 'f(x2)', 'Ea'})
Results = 5×8 table
Iteration x0 x1 x2 f(x0) f(x1) f(x2) Ea _________ ______ ______ ______ _________ _________ ___________ ______ 1 3 4 4.6 -16 -6 7.296 0.6 2 4 4.6 4.2708 -6 7.296 -0.79078 0.3292 3 4.6 4.2708 4.303 7.296 -0.79078 -0.087734 0.0322 4 4.2708 4.303 4.307 -0.79078 -0.087734 0.0012921 0.004 5 4.303 4.307 4.3069 -0.087734 0.0012921 -2.0587e-06 0.0001
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 commentaires
Christoppe
Christoppe le 27 Juin 2022
Hello sir, Thank you so much for your assistance, it means a lot to us who don't have enough knowledge in MATLAB. I hope I can return the favor someday...
Star Strider
Star Strider le 27 Juin 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!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by