MATLAB: Ode4 gives variable undefined error

fexode4variable undefined

I am trying to use a fixed step solver, such as ODE4 to calculate a value after a fixed time step, however, I am getting stuck on a very basic point. I have to pass F to the ode solver however, obviously because it is an ode, I have the variable that I need to find in F so it is giving me " variable undefined" error. This error is obvious to me but is there a way around it?
I am using the following function from mathworks – ode4 . Please find the code in the functions tab.
And my function calling statement is –
t = linspace(1,10)
vp(1) = 5 %some constant that changes in the loop below
for i = 1: length(t)
v = ode4((v - vf), t(i), 1, t(i+1),vp(1));
vp(1) = 45; % some updated value ( I've chosen a random number for the time being)
end
%error displayed
% -->> Undefined function or variable 'v'.

Best Answer

First of all you have to make sure that you call ode4 with the inputs it expects. In your call you do not have any function. According to the ode4 information it expects a call looking like this:
ode4(F,t0,h,tfinal,y0)
Inside ode4 you will see that F is used like this:
s1 = F(t,y);
This means that F has to be a function of t and y, and those 2 variable only. For example:
F = @(t,y) 2*exp(-(y-0.32*t).^2/4)
Then you can integrate the ode:
from 0 to 12 in steps of 1/4 something like this:
Y0to12 = ode4(F,0,1/4,12,0);