Transforming long symbolic expressions to use in matlab ode45?

Click For Summary

Discussion Overview

The discussion revolves around the challenges of using symbolic expressions in MATLAB, specifically for integration with the ode45 function. Participants explore methods to directly incorporate long symbolic expressions into a function without needing to manually copy and paste from the command window.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a function that outputs a long symbolic expression and seeks a way to integrate it directly within the ode45 function without external manipulation.
  • Another participant suggests using the subs function to evaluate symbolic expressions but notes an error indicating that inputs must be floats.
  • Some participants propose using eval() as a potential solution for evaluating dynamic expressions, though they acknowledge it may not be efficient.
  • There are mentions of trying various functions like vpa and double without success in resolving the issue.
  • One participant suggests posting the question on the MathWorks forums for expert advice.
  • A later reply provides an example of how to structure the function to include symbolic expressions, emphasizing the need to pass an anonymous function to ode45.

Areas of Agreement / Disagreement

Participants express various methods and ideas, but there is no consensus on a definitive solution to the problem of integrating symbolic expressions directly in MATLAB. Multiple competing views and approaches remain unresolved.

Contextual Notes

Limitations include the dependence on the specific structure of the symbolic expressions and the requirement for numeric inputs in the ode45 function. The discussion does not resolve the mathematical steps necessary for successful implementation.

patric44
Messages
308
Reaction score
40
TL;DR
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: patric44

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K