I should write a MATLAB function that takes a first order ordinary differential equation in form y’(t) = a*y(t) +b with an initial point y(t0)=y0 as inputs and calculates first 15 points of the solution. Also draws the solution curve for first 15 points.
And the equation that we want to solve is ;y’(t) = 4*y(t)+1 with the initial point y(0)=0.
For this function I wrote the bellowing code but this gives me an error about y. How should I implement the euler function correctly? And also I could not determine how I can draw the solution curves..
function E=euler(f,y)
%Input - f is the function entered as a string 'f'
% - a and b are the left and right endpoints
% - ya is the initial condition y(a)
% - M is the number of steps
%Output - E=[T' Y'] where T is the vector of abscissas and
% Y is the vector of ordinates
h=0.1;
y(0)=0;
for j=0:15
Y(j+1)=Y(j)+h*feval(4*(y(t)+1));
end
Patch:
Well, I am not sure about the mathematical part, but – The indices need to start at “1”. Other then e.g. in C, you must not use “0” as an index.