MATLAB Transforming long symbolic expressions to use in matlab ode45?

AI Thread Summary
The discussion revolves around the challenge of integrating long symbolic expressions in MATLAB using the ode45 function. The user is attempting to directly incorporate symbolic expressions generated by a function into the ode45 integrator without needing to copy and paste from the command window. Various methods such as subs, vpa, and eval have been tried but have not yielded successful results. A suggestion was made to replace symbolic variables with their numeric counterparts directly within the function passed to ode45, using an anonymous function to handle additional parameters. The conversation highlights the need for a more efficient way to manage symbolic expressions in MATLAB for numerical integration.
patric44
Messages
308
Reaction score
40
TL;DR Summary
how can i transform a long symbolic expression to use in Matlab ode45?
hi guys
i build a little function that outputs a symbolic expression like that below, this is only a part of it:
[CODE lang="matlab" title="the symbolic expression"] (105*(x(5)^2/r^2 - 1)^2)/8 + (210*x(5)^2*(x(5)^2/r^2 - 1))/r^2))/(r^2)^(9/2) + (8*R^7*u*x(5)[/CODE]
the problem is the only way to integrate this output is to expand the function externally in the command window of Matlab and then
copy and paste it to the function that contains the ode45 integrator, i also tried to use vpa like this
[q,c] = myfunction(x,...)
Q= vpa(q)
C= vpa(c)
why i had to copy and paste the expression from the command window first!, is there's a way to internally expand this and make it usable in the ode45?
 
Physics news on Phys.org
jedishrfu said:
i tried to use subs but it gave me the following error
? Error using ==> odearguments at 120
Inputs must be floats, namely single or double.
this is a general shape of the function

Matlab:
function dx = function(t,x)
[U,a,b,c] = expansion(2,sym('x'),sym('m/z'));
A =subs(a)
B =subs(b)
C =subs(c)
%%
    dx=[
        x(2)
        ;A
        ;x(4)
        ;B
        ;x(6)
        ;C
        ];
end
and the other function for the integrator
Matlab:
x0 = [1;2;3];
opts = odeset; opts.RelTol = 10^-9;
[t2,y2] = ode45(@function,[0:60:5000],x0,opts);

why the subs didn't work, or i was using it in a wrong way
 
any help on this guys, this is very important for me i will appreciate any help
 
Your best bet is to also ask on the mathworks.com forums as Mathworks have people monitoring their forums and who are subject matter experts in MATLAB.
 
  • Like
Likes patric44
I'm not sure what you mean by expanding the expression.

If the expression is dynamic, you might try something like eval(). This allows you to pass a string or other variable into an expression to be evaluated.

It's often not the most efficient method.
 
onatirec said:
I'm not sure what you mean by expanding the expression.

If the expression is dynamic, you might try something like eval(). This allows you to pass a string or other variable into an expression to be evaluated.

It's often not the most efficient method.
what I mean is that I have a function that outputs long symbolic expressions and for convenient reasons i used the the subs function inside it to replace x by sym('x(1)') and y by sym('x(3)') and so on, in order to implement the expression inside a function that will be integrated using the ode45.
the problem is that I have to excute the function first in the command window then copy the long expression that contains the the symbolic x(1),x(2)... into the dx function then use the ode45, what i want to do is to obtain the long symbolic expression directly inside the dx function without needing the command window.
 
the eval command also didn't work, I tried vpa,double,subs and also didn't work?!
 
Have you tried posting your question on the MATLAB site? There are a lot of MATLAB people who will likely know the answer.
 
  • Like
Likes patric44
  • #10
jedishrfu said:
Have you tried posting your question on the MATLAB site? There are a lot of MATLAB people who will likely know the answer.
I am trying to post right now I hope I find something wish me luck
 
  • Like
Likes jedishrfu
  • #11
As I understand it, you have three symbolic expressions involving x,y,z and m and you want to calculate their numeric values given numeric values for (x,_1, x_2, x_3, x_4, x_5, x_6) = (x,\dot x, y, \dot y, z, \dot z) and m.

Replacing x with the symbolic form of x_1 etc. doesn't appear to assist you with that; rather the documentation suggests that you want something like
Matlab:
% s1, s2, and s3 are symbolic expressions
function dx = f(t,x, m)
   dx = [
      x(2);
      subs(s1, [sym('x'),sym('y'), sym('z'), sym('m')], [x(1),x(3),x(5),m]);
      x(4);
      subs(s2, [sym('x'),sym('y'), sym('z'), sym('m')], [x(1),x(3),x(5),m]);
      x(6);
      subs(s3, [sym('x'),sym('y'), sym('z'), sym('m')], [x(1),x(3),x(5),m]);
]
end

That is a function of three variables, so in ode45 you need to pass an anonymous function:
Matlab:
[t,x] = ode45(@(t,x) f(t,x,m), ...)
 
  • Informative
Likes patric44

Similar threads

Back
Top