Transforming long symbolic expressions to use in matlab ode45?

  • #1
patric44
272
37
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?
 

Answers and Replies

  • #3
patric44
272
37
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
patric44
272
37
any help on this guys, this is very important for me i will appreciate any help
 
  • #5
14,070
8,025
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.
 
  • #6
onatirec
86
77
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
patric44
272
37
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
patric44
272
37
the eval command also didn't work, I tried vpa,double,subs and also didn't work?!
 
  • #9
14,070
8,025
Have you tried posting your question on the MATLAB site? There are a lot of MATLAB people who will likely know the answer.
 
  • #10
patric44
272
37
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
 
  • #11
pasmith
Homework Helper
2,448
1,054
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), ...)
 

Suggested for: Transforming long symbolic expressions to use in matlab ode45?

Replies
0
Views
598
Replies
6
Views
1K
Replies
1
Views
701
  • Last Post
Replies
2
Views
539
Replies
1
Views
385
  • Last Post
Replies
1
Views
595
Replies
7
Views
1K
  • Last Post
Replies
9
Views
832
Replies
8
Views
788
  • Last Post
Replies
2
Views
720
Top