MATLAB: Solving non homogenous differential equations numerically using ode45 etc

nonhomogenousode45

How is a non homogenous differential equation solved in MATLAB using ode45 or ode23. I have a function like:- dmdt = a*exp(Asin(wt) + (2-m)^2);
Can I obtain the numerical solution for this?
Thanks in advance

Best Answer

Sarah, yes you can. The typical approach for such an example is to create two functions:
function my_EOM()
a = 1;
A = 1;
w = 1;
fun = @(w,x) sin(w.*x);
param = {a; A; w; fun};
IC = -1;
[t,m] = ode45(@EOM,[0 1],IC,[],param);
plot(t,m)
xlabel('t')
ylabel('m')
grid
end
function dmdt = EOM(t, m, param)
a = param{1};
A = param{2};
w = param{3};
fun = param{4};
dmdt = a*exp(A*fun(w,t) + (2 - m)^2);
end
Save both functions in the same .m-file and with name my_EOM.m. Execute and enjoy.
Related Question