saving outputs from a loop

1 visualizzazione (ultimi 30 giorni)
Tanner Smith il 9 Mag 2020
Commentato: Tanner Smith il 9 Mag 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
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposta accettata

Ajay Kumar il 9 Mag 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
1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Tanner Smith il 9 Mag 2020

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by