Usage Of "goto" for previous lines
Ältere Kommentare anzeigen
hello guys Iva had a simple but confusing question... i was writing a program for Euler's formula, then I found out Matlab doesn't have any "goto" function for refering to previous lines . i was looking for an alternate and i will appreciate it if you guys help me with this :) here's an example:
x=[0.1:0.1:1]
h=0.1
i=1
y(1)=1
Z(1)=1.1
Y(1)=1.1
%Y'=y*(x^(1/3))
while i<11
y(i+1)=y(i)+(h*(x(i)*(y(i)^(1/3))))
Z(i+1)=(x(i+1)*(y(i+1)^(1/3)))
line** y(i+1)=y(i)+(0.5*h*(Y(i)+Z(i+1)))
Y(i+1)=x(i+1)+y(i+1)
if abs(Y(i+1)-Z(i+1))>0.00000000001
Z(i+1)=Y(i+1)
goto(line**)
end
i=i+1
end
Akzeptierte Antwort
Weitere Antworten (1)
Try this:
x = 0.1:0.1:1;
h = 0.1;
y(1) = 1;
Z(1) = 1.1;
Y(1) = 1.1;
%Y'=y*(x^(1/3))
for k = 1:9
y(k+1) = y(k)+(h*(x(k)*(y(k)^(1/3))));
Z(k+1) = (x(k+1)*(y(k+1)^(1/3)));
while true
y(k+1) = y(k)+(0.5*h*(Y(k)+Z(k+1)));
Y(k+1) = x(k+1)+y(k+1);
if abs(Y(k+1)-Z(k+1))<=0.00000000001, break, end
Z(k+1) = Y(k+1);
end
end
Note that this kind of code style can easily end up in an infinite loop, so you might want to consider how to prevent this. And to be honest this looks like a good contender for some serious rewriting as proper MATLAB code, i.e. fully vectorized .
Kategorien
Mehr zu Whos finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!