Return function to go back to the beginning of the script

9 ビュー (過去 30 日間)
Furkan Erdem
Furkan Erdem 2021 年 4 月 28 日
編集済み: Alan Stevens 2021 年 4 月 28 日
x=50;
t=0:x:1000;
n=numel(t);
h(1)=100;
es=0.1;
for i=1:n-1;
f1=2-0.1*(h(i)^(1/2));
h(i+1)=h(i)+x*f1;
f2=2-0.1*(h(i+1)^(1/2));
e(i+1)=((f2-f1)/2)*x;
if e(i+1)> es
x=x/10;
return
end
end
t1=transpose(t);
h1=transpose(h);
e1=transpose(abs(e));
T=table(t1,h1,e1)
I wanted to calculate the error in the presented Euler's Method code. Error is supposed to be less than 0.1, and I want to change x to one tenth of x if error is above 0.1. Can't make the code return the beginning to execute the whole script again. How should I proceed?

採用された回答

Alan Stevens
Alan Stevens 2021 年 4 月 28 日
編集済み: Alan Stevens 2021 年 4 月 28 日
Make the euler routine a function and call it from a while loop. Along the lines of:
...
while error>0.1
[h, error] = Euler(t,h1);
end
...
function [h, error] = Euler(t,h1)
% Euler calcs
h = ...
error = ...
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by