Asked by Tejas Adsul
on 18 May 2018

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!

Answer by 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.

Tejas Adsul
on 23 May 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401448-transferring-a-symbolic-expression-to-a-odefun#comment_571347

## Tejas Adsul (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401448-transferring-a-symbolic-expression-to-a-odefun#comment_571349

Sign in to comment.