saving outputs from a loop

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by