saving outputs from a loop

1 回表示 (過去 30 日間)
Tanner Smith
Tanner Smith 2020 年 5 月 9 日
コメント済み: Tanner Smith 2020 年 5 月 9 日
I would like to be able to run this program but save each ouput in the loop for both the x and the y in a matrix or array. this will find all the solutions but i don't know how to make it save each individual solution so I could graph it if I wanted to.
%matlab code for euler's method
clear all
clc
f=@(x,y)(x^2)/(1+y^2)+2*y;
x0=input('Enter initial value of x or x(0): ');%you could put x(2) here instead for this problem
y0=input('Enter initial value of y or y(0): ');%you could put y(2) here instead for this problem
xn=input('Enter the final value of x(n): '); %so 3 for this problem
h=input('Enter the step length h: '); %so h=.01
fprintf(' x y ');
while x0<=xn
fprintf('\n%4.3f %4.3f ',x0,y0);
y1=y0+h*f(x0,y0);
x1=x0+h;
x0=x1;
y0=y1;
end

採用された回答

Ajay Kumar
Ajay Kumar 2020 年 5 月 9 日
%matlab code for euler's method
clear all
clc
f=@(x,y)(x^2)/(1+y^2)+2*y;
x0=input('Enter initial value of x or x(0): ');%you could put x(2) here instead for this problem
y0=input('Enter initial value of y or y(0): ');%you could put y(2) here instead for this problem
xn=input('Enter the final value of x(n): '); %so 3 for this problem
h=input('Enter the step length h: '); %so h=.01
fprintf(' x y ');
i = 1;
while x0<=xn
fprintf('\n%4.3f %4.3f ',x0,y0);
y1(i)=y0+h*f(x0,y0);
x1(i)=x0+h;
x0=x1(i);
y0=y1(i);
i = i+1;
end
  1 件のコメント
Tanner Smith
Tanner Smith 2020 年 5 月 9 日
thank you for your help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by