MATLAB: Handling with long function in ode45

functionsMATLABode45

Hi.
I have a problem with operating a long function in ode45:
[t,y] = ode45(@(t,y) [(V^2*A/((p_r(1)*y(1)+p_r(2))*1e-6)-(0.1558*y(1) +89.33)*Ac*(y(1)-Ta))/(m*(p_c(1)*y(1)^2+p_c(2)*y(1)+p_c(3))); -((V^2*A/((p_r(1)*y(1)+p_r(2))*1e-6)-(0.1558*y(1) +89.33)*Ac*(y(1)-Ta))/(m*(p_c(1)*y(1)^2+p_c(2)*y(1)+p_c(3))))*(0.5*ksiM*aA*omega*sin(aA*(y(1)-As)+bA*y(2))*(1/(1+exp(-s*(y(1)-As)))-1/(1+exp(-s*(y(1)-Af))))-theta)/(1+0.5*ksiM*bA*omega*sin(aA*(y(1)-As)+bA*y(2))*(1/(1+exp(-s*(y(1)-As)))-1/(1+exp(-s*(y(1)-Af)))))] , [0 3], [20;0]);
As you can see the function is very long, in fact it is even a system of two. Would it be possible to write a piece of this function, for example:
sin(aA*(y(1)-As)+bA*y(2))
as simple expression, let's say:
f
and use this short expression inside ode45 instead of much longer sine? This would make it much easier for me to operate the function.
Kind regards
Olaf

Best Answer

Yes, you can use anonymous functions.
You should consider using a real function file for the expressions as you can use as many variables as you like to make the code clearer or more efficient.
If you have the symbolic toolbox then you can even use matlabFunction to write optimized code to a file.
Related Question