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 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