saving outputs from a loop

1 view (last 30 days)
Tanner Smith
Tanner Smith on 9 May 2020
Commented: Tanner Smith on 9 May 2020
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

Accepted Answer

Ajay Kumar
Ajay Kumar on 9 May 2020
%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

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!