Transforming long symbolic expressions to use in matlab ode45?

In summary: This will give you the 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: [t,x] = ode45(@(t,x) f(t,x,m), ...)In summary, you need to expand the function first in the command window, then copy and paste it into the function that contains the ode45 integrator.
  • #1
patric44
308
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:
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)
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
  • #3
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
 
  • #4
any help on this guys, this is very important for me i will appreciate any help
 
  • #5
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
  • #6
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.
 
  • #7
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.
 
  • #8
the eval command also didn't work, I tried vpa,double,subs and also didn't work?!
 
  • #9
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 [itex]x,y,z[/itex] and [itex]m[/itex] and you want to calculate their numeric values given numeric values for [itex](x,_1, x_2, x_3, x_4, x_5, x_6) = (x,\dot x, y, \dot y, z, \dot z)[/itex] and [itex]m[/itex].

Replacing [itex]x[/itex] with the symbolic form of [itex]x_1[/itex] 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

FAQ: Transforming long symbolic expressions to use in matlab ode45?

1. What is the purpose of transforming long symbolic expressions for use in Matlab ode45?

The purpose of transforming long symbolic expressions for use in Matlab ode45 is to simplify and optimize the integration process for solving differential equations. This allows for more efficient and accurate numerical solutions.

2. How do I transform a symbolic expression for use in Matlab ode45?

To transform a symbolic expression, you can use the "subs" function in Matlab which substitutes numerical values for symbolic variables. You can also use the "matlabFunction" function to convert the expression into a function that can be used with ode45.

3. Can I use any type of symbolic expression with Matlab ode45?

Yes, you can use any type of symbolic expression as long as it can be converted into a function that can be evaluated numerically. This includes algebraic expressions, trigonometric functions, and special functions.

4. What are the benefits of using symbolic expressions with Matlab ode45?

Using symbolic expressions with Matlab ode45 allows for more flexibility and control over the integration process. It also allows for easy manipulation and modification of the equations without having to rewrite the entire code.

5. Are there any limitations to using symbolic expressions with Matlab ode45?

One limitation is that the integration process may be slower compared to using numerical expressions. Additionally, symbolic expressions may not be suitable for solving complex or highly nonlinear differential equations.

Similar threads

Replies
8
Views
2K
Replies
2
Views
3K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
5
Views
4K
Replies
1
Views
2K
Back
Top