Transferring a symbolic expression to a odefun

2 views (last 30 days)
By some code, I have these 6 ODEs in the command window.
dEm_dx=
2*x3 - 2*x2 - x4 - y3/2 + 1/4
2*x2 - 2*x3 + 2*x4 + y2/2 - y4/2 - 1
2*x3 - x2 - 2*x4 + y3/2 + 3/4
x3/2 - 2*y2 + 2*y3 - y4 + 1/2
x4/2 - x2/2 + 2*y2 - 2*y3 + 2*y4 - 1
2*y3 - y2 - x3/2 - 2*y4 + 1
The number of these equations depend on a parameter 'n', which is always odd.
"Number of equations = 2*(n-2)" So, here, n=5. If n was 7, I would have 10 equations.
Here, x2, x3, x4, y2, y3, y4 are symbolic variables (not functions, since I cannot create symbolic functions in a loop), which I created using a loop, so that just by putting the value of 'n', I would get appropriate number of variables. Like this:
x = sym('x',[n,1]);
y = sym('y',[n,1]);
Now, I want to solve these ODEs numerically. This is the code:
tspan = 0:0.01:3
init = rand(2*(n-2),1);
[t,X] = ode45(@odefun, tspan, init);
function ode = odefun(t,X)
global n
ode = zeros(2*(n-2),1);
x2=X(1); x3=X(2); x4=X(3);
y2=X(4); y3=X(5); y4=X(6);
ode(1) = 2*x3 - 2*x2 - x4 - y3/2 + 1/4;
ode(2) = 2*x2 - 2*x3 + 2*x4 + y2/2 - y4/2 - 1;
ode(3) = 2*x3 - x2 - 2*x4 + y3/2 + 3/4;
ode(4) = x3/2 - 2*y2 + 2*y3 - y4 + 1/2;
ode(5) = x4/2 - x2/2 + 2*y2 - 2*y3 + 2*y4 - 1;
ode(6) = 2*y3 - y2 - x3/2 - 2*y4 + 1;
end
The problem is, I had to copy those equations one by one from command window to function script, since the two workspaces are different. I also had to write x2=X(1); x3=X(2) ...
I would like the function to automatically take the ODEs from dEm_dx. I know there are disagreements between variable types. ode45 cannot take symbolic variables. So, I want to transform those equations in some way so that ode45 can understand them. Moreover, this should happen in a loop. Something like
for i=1:2*(n-1)
%some code for transforming x2,x3,etc from symbolic variables to numeric variables
ode(i) = dEm_dx(i);
end
odeFunction and matlabFunction can't be used in a loop, as far as I understand. I cannot create an array of function handles. I also do not want to use dsolve since I want to solve them numerically.
I'm not sure this is the best way to go about solving those equations numerically. Any other techniques you can share would be really helpful. But I would really appreciate if someone would guide me through this.
Thank you!
  2 Comments
Walter Roberson
Walter Roberson on 23 May 2018
I showed you in https://www.mathworks.com/matlabcentral/answers/401056-creating-multiple-symbolic-functions-with-arguments that you can create symbolic functions in a loop. However you would need to store them in cell array entries.
Tejas Adsul
Tejas Adsul on 23 May 2018
Oh. That line in the question is unnecessary I guess. I do want x2, x3, etc to be symbolic variables. This is because I differentiate an expression with respect to x2, x3, etc to get those ODEs.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 18 May 2018
If you want to solve the ODEs numerically, read through the examples for the odeFunction function. There's another example on this documentation page.
If you want to solve the ODEs symbolically, look at the dsolve function and the examples on this documentation page.
  1 Comment
Tejas Adsul
Tejas Adsul on 23 May 2018
None of them have an example similar to my case. I have number of variables and number of equations dependent on 'n'. Maybe I'll edit my question to reduce a bit of the mess.

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!