I'm new in Matlab, so sorry for asking a banal and probably already answered question, but I wasn't able to solve it.
I have this ODE to solve
g''(x)+f(x)g'(x)
with g(0)=0 and g'(0)=0.5
f(x) is a known vector, so I tried
xspan = linspace(0,10,100);
y0 = [0,0.5];
%f=f(xspan), known function, for example f=1./xspan;
[x,y] = ode45('odefun',xspan,y0);
and for odefun
function dy=odefun(x,y)
global f
dy(1) = y(2);
dy(2) = -f.*y(2);
dy = dy(:);
end
Of course it doesn't work. How can I pass the values of vector f
to the solver?
EDIT
Thanks for help, but this is not working for me. Maybe I have an old Matlab version (7.11.0 (R2010B))? I tried to do as you told, but this is what I get in command window
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> odefun at 3
dy(2) = -f.*y(2);
Error in ==> @(x,y)odefun(x,y,f)
Error in ==> odearguments at 109
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> trying at 7
[x,y] = ode45(fun,xspan,y0);
This is the code trying.m
xspan=linspace(0,10,100);
y0=[0 0.5];
f=1./xspan;
[x,y] = ode45(@(x,y)odefun(x,y,f),xspan,y0);
and the odefun
function dy=odefun(x,y,f)
dy(1) = y(2);
dy(2) = -f.*y(2);
dy = dy(:);
end