I am writing code to do improved euler method on a 1st order ODE. My difficulty is in the declaration of the function. While the syntax seems correct matlab does not know whether to handle this as a vector and use .^2 or use ^2 when squaring x and y. I purposely did not use a function saved on disk; I want all code to be contained within m file.
I would also like to output 2 columns X and Y next to each other at the end with column headers.
Any suggestions? Part of this code was copied from Edwards and Penny Elementary Diffeq. Thanks. MM
Matlab code:
clc; clear; clear axes; clf; close all;
%Inputs
x(1)=0; y(1)=1;
f=@(x,y)( y+sqrt(x^2+y^2) );
a=1; b=3; n=3;
h=(b-a)/n;
%
X=x;
Y=y;
for i=1:n;
k1=f(x,y);
k2=f(x+h,y+h*k1);
k=(k1+k2)/2;
x=x+h;
y=y+h*k;
X=[X;x];
Y=[Y;y];
end
% output X and Y
table=[X,Y]
table'