Substituing symbolic variable with an element of a matrix
3 次查看(过去 30 天)
显示 更早的评论
So, I have this ODE system (named Y):
Y=
2.09e-4*thetaxpp*cos(4.0*sin(3.0*t)) - 9.16e-4*thetaypp*sin(4.0*sin(3.0*t)) - 0.00125*thetayp*cos(3.0*t)*cos(4.0*sin(3.0*t)) - 0.00125*thetaxp*cos(3.0*t)*sin(4.0*sin(3.0*t))
2.09e-4*thetaypp*cos(4.0*sin(3.0*t)) + 9.16e-4*thetaxpp*sin(4.0*sin(3.0*t)) + 0.00125*thetaxp*cos(3.0*t)*cos(4.0*sin(3.0*t)) - 0.00125*thetayp*cos(3.0*t)*sin(4.0*sin(3.0*t))
3.54e-4*cos(4.0*sin(3.0*t))*sin(4.0*sin(3.0*t))*thetaxp^2 + 3.54e-4*cos(4.0*sin(3.0*t))*sin(4.0*sin(3.0*t))*thetayp^2 + 2.09e-4*thetazpp - 1.11*sin(3.0*t)
Now, I want to use the ode45i function on it, which means that I want to do:
f2=@(t,y,yp)(Y,[0 10],y0,yp0)
To do so, I need to perform these substitutions (substitute a symbolic variable with an element of a matrix) before implementing Y in the function ode15i:
thetaxpp=yp(1)
thetaypp=yp(2)
thetazpp=yp(3)
thetaxp=y(1)
thetayp=y(2)
thetazp=y(3)
Now, I've tried to do:
f2=@(t,y,yp)(subs(Y,[thetaxpp, thetaypp, thetazpp, thetaxp, thetayp, thetazp],[yp(1), yp(2) , yp(3), y(1), y(2), y(3)]))
but I get the message
Error using odearguments (line 101)
Inputs must be floats, namely single or double.
I also tried isolating the equations of Y using fprintf (to transform the equations into text before doing the substitution and to transform them again into equations after) like this:
formatSpec = '\n %4.2f \n %4.3f \n %4.4f\n';
fprintf(formatSpec,Y(1),Y(2),Y(3))
but I got the message:
Error using fprintf
Function is not defined for 'sym' inputs.
Do you guys have any idea what I could do to solve my problem?
Thanks a bunch
0 个评论
回答(1 个)
Star Strider
2016-6-27
You don’t need to perform the substitutions! Let MATLAB do it! Use the matlabFunction function, and create an anonymous function to use with your ODE.
See if this works for you:
syms thetaxpp thetaypp thetaxp thetayp thetazpp thetazp t
Y = [2.09e-4*thetaxpp*cos(4.0*sin(3.0*t)) - 9.16e-4*thetaypp*sin(4.0*sin(3.0*t)) - 0.00125*thetayp*cos(3.0*t)*cos(4.0*sin(3.0*t)) - 0.00125*thetaxp*cos(3.0*t)*sin(4.0*sin(3.0*t))
2.09e-4*thetaypp*cos(4.0*sin(3.0*t)) + 9.16e-4*thetaxpp*sin(4.0*sin(3.0*t)) + 0.00125*thetaxp*cos(3.0*t)*cos(4.0*sin(3.0*t)) - 0.00125*thetayp*cos(3.0*t)*sin(4.0*sin(3.0*t))
3.54e-4*cos(4.0*sin(3.0*t))*sin(4.0*sin(3.0*t))*thetaxp^2 + 3.54e-4*cos(4.0*sin(3.0*t))*sin(4.0*sin(3.0*t))*thetayp^2 + 2.09e-4*thetazpp - 1.11*sin(3.0*t)];
Yfcn = matlabFunction(Y);
You will have to add ‘y’ as the second argument in the argument list of ‘Yfcn’ to create:
Yfcn = @(t,y,thetaxp,thetayp,thetaxpp,thetaypp,thetazpp)[thetaxpp.*cos(sin(t.*3.0).*4.0).*2.09e-4-thetaypp.*sin(sin(t.*3.0).*4.0).*9.16e-4-thetayp.*cos(sin(t.*3.0).*4.0).*cos(t.*3.0).*(1.0./8.0e2)-thetaxp.*sin(sin(t.*3.0).*4.0).*cos(t.*3.0).*(1.0./8.0e2);thetaypp.*cos(sin(t.*3.0).*4.0).*2.09e-4+thetaxpp.*sin(sin(t.*3.0).*4.0).*9.16e-4+thetaxp.*cos(sin(t.*3.0).*4.0).*cos(t.*3.0).*(1.0./8.0e2)-thetayp.*sin(sin(t.*3.0).*4.0).*cos(t.*3.0).*(1.0./8.0e2);thetazpp.*2.09e-4-sin(t.*3.0).*(1.11e2./1.0e2)+thetaxp.^2.*cos(sin(t.*3.0).*4.0).*sin(sin(t.*3.0).*4.0).*3.54e-4+thetayp.^2.*cos(sin(t.*3.0).*4.0).*sin(sin(t.*3.0).*4.0).*3.54e-4];
tspan = [ ... ];
initcond = [0; 0; 0];
[t,y] = ode15i(@(t,y) Yfcn(t,y,thetaxp,thetayp,thetaxpp,thetaypp,thetazpp), tspan, initcond);
Be certain to define:
thetaxp,thetayp,thetaxpp,thetaypp,thetazpp
in your workspace first.
NOTE — This is UNTESTED CODE but it should work if I understand your Question correctly.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!