Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How do I use a for loop?

1 Ansicht (letzte 30 Tage)
James Pain
James Pain am 17 Mär. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Hello, I'm currently trying to make an approximation method in matlab, and this is what I have currently:
function T = Dog(A,S)
L=randi([-9,9],3,1);
H=L/norm(L);
O=(inv((A-(A(3,3)*eye(3)))))*(H);
Y=O/norm(O);
G=(inv((A-(A(3,3)*eye(3)))))*(Y);
D=G/norm(G);
F=(inv((A-(A(3,3)*eye(3)))))*(D);
R=F/norm(F);
W=(inv((A-(A(3,3)*eye(3)))))*(R);
X=W/norm(W);
V=(inv((A-(A(3,3)*eye(3)))))*(X);
K=V/norm(V);
N=(K.*K)./(K.*V);
T=N+S;
end
Now I was wondering is there someway of using a for loop from lines 3 to 13, so that instead of having to change the name of the variable each time it would simply replace it with with the one that came before it?
  1 Kommentar
James Pain
James Pain am 17 Mär. 2017
I had the Approximation method wrong originally, but I've now edited the question properly.

Antworten (1)

Thorsten
Thorsten am 17 Mär. 2017
Bearbeitet: Thorsten am 17 Mär. 2017
F=randn(3,1)./W;
for i = 1:5
Q = inv(A - S*eye(3))*F;
F = W./Q;
W = Q;
end
J = (F.*F)./(Q.*F);
T = J + S;
  3 Kommentare
Jan
Jan am 17 Mär. 2017
Bearbeitet: Jan am 17 Mär. 2017
@James: The reply remains almost the same. Can you apply the required changes by your own?
You can create C = A - S*eye(3) once before the loop. Then for numerical stability replace the explicit inversion by:
Q = C \ F;
James Pain
James Pain am 17 Mär. 2017
Your Right, I've figured out how to do it, cheers

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by