Transforming long symbolic expressions to use in matlab ode45?

Click For Summary
SUMMARY

The discussion focuses on integrating long symbolic expressions in MATLAB using the ode45 function. The user encountered issues with the 'subs' function, which resulted in an error indicating that inputs must be floats. To resolve this, it is suggested to use symbolic expressions directly within the function and replace variables using 'subs' with numeric values passed to the ode45 function. Additionally, the use of 'eval()' was mentioned, but it is not recommended due to efficiency concerns.

PREREQUISITES
  • Familiarity with MATLAB symbolic toolbox
  • Understanding of the ode45 function for numerical integration
  • Knowledge of symbolic variable substitution using 'subs'
  • Basic understanding of function handles in MATLAB
NEXT STEPS
  • Research how to implement anonymous functions in MATLAB for ode45
  • Learn about the 'subs' function in MATLAB for symbolic variable replacement
  • Explore the use of the 'eval()' function and its implications on performance
  • Investigate best practices for handling symbolic expressions in MATLAB
USEFUL FOR

MATLAB users, engineers, and researchers working with symbolic mathematics and numerical integration, particularly those utilizing the ode45 function for solving differential equations.

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