Not Sure How to Fix Error where I Define Function
1 view (last 30 days)
Show older comments
Hello,
I am not sure how to fix my function in my script.
Attached is a sample of my code:
clc; clear all;
ti = 0;
tf = 15;
global I11 I22 I33 Mx My Mz w10 w20 w30 eps10 eps20 eps30 eps40...
C110 C120 C130 C210 C220 C230 C310 C320 C330 IC K0
I11 = 160;
I22 = 400;
I33 = 400;
Mx = 0;
My = 0;
Mz = 45;
w10 = 2;
w20 = -1;
w30 = 1;
eps10 = 0;
eps20 = 0;
eps30 = 0;
eps40 = 1;
K0 = eps10^2+eps20^2+eps30^2+eps40^2;
C110 = 1-2*eps20^2-2*eps30^2;
C120 = 2*(eps10*eps20-eps30*eps40);
C130 = 2*(eps30*eps10+eps20*eps40);
C210 = 2*(eps10*eps20+eps30*eps40);
C220 = 1-2*eps30^2-2*eps10^2;
C230 = 2*(eps20*eps30-eps10*eps40);
C310 = 2*(eps30*eps10-eps20*eps40);
C320 = 2*(eps20*eps30+eps10*eps40);
C330 = 1-2*eps10^2-2*eps20^2;
IC = [w10 w20 w30...
eps10 eps20 eps30 eps40...
C110 C120 C130 C210 C220 C230 C310 C320 C330];
opts = odeset('RelTol', 1*10^(-10),'AbsTol', 1*10^(-10));
[t, y] = ode45(@(t,y) DynEqn1(t,y,I11,I22,I33,Mx,My,Mz), [ti tf], IC, opts);
N = sqrt(sum(y(:,4:7).^2,2));
kap = acosd(1-2*y(:,5)^2-2*y(:,7)^2);
phi1 = acosd((2*(y(:,4)*y(:,5)+y(:,6)*y(:,7)))/sind(kap));
phi2 = asind((2*(y(:,6)*y(:,4)-y(:,5)*y(:,7)))/sind(kap));
if phi1==phi2
phi = phi1;
elseif phi1==180-phi2
phi = phi1;
elseif -phi1==phi2
phi = -phi1;
elseif -phi1==180-phi2
phi = -phi1;
else
disp('Something is wrong with phi')
figure (1)
plot(t,phi)
figure (2)
plot(t,kap)
figure (3)
plot(y(:,11),y(:,14))
function soln = DynEqn1(t,y,I11,I22,I33,Mx,My,Mz)
w1 = y(1);
w2 = y(2);
w3 = y(3);
eps1 = y(4);
eps2 = y(5);
eps3 = y(6);
eps4 = y(7);
C11 = y(8);
C12 = y(9);
C13 = y(10);
C21 = y(11);
C22 = y(12);
C23 = y(13);
C31 = y(14);
C32 = y(15);
C33 = y(16);
w1_dot = (Mx - w2*w3*(I33-I22))/I11;
w2_dot = (My - w1*w3*(I11-I33))/I22;
w3_dot = (Mz - w1*w2*(I22-I11))/I33;
eps1_dot = .5*(w1*eps4-w2*eps3+w3*eps2);
eps2_dot = .5*(w1*eps3+w2*eps4-w3*eps1);
eps3_dot = .5*(-w1*eps2+w2*eps1+w3*eps4);
eps4_dot = -.5*(w1*eps1+w2*eps2+w3*eps3);
C11_dot = C12*w3-C13*w2;
C12_dot = C13*w1-C11*w3;
C13_dot = C11*w2-C12*w1;
C21_dot = C22*w3-C23*w2;
C22_dot = C23*w1-C21*w3;
C23_dot = C21*w2-C22*w1;
C31_dot = C32*w3-C33*w2;
C32_dot = C33*w1-C31*w3;
C33_dot = C31*w2-C32*w1;
soln = [w1_dot; w2_dot; w3_dot; ...
eps1_dot; eps2_dot; eps3_dot; eps4_dot; ...
C11_dot; C12_dot; C13_dot; C21_dot; C22_dot; C23_dot; C31_dot; C32_dot; C33_dot];
end
I get this at the line where I begin the function:
Function definitions are not permitted in this context.
I double checked that I had the correct number of initial conditions that match the number of solutions I defined.
Furthermore, this code was basicaly just an edit from a previous code of mine, where all I did was add C11 through C33 and C11_dot through C33_dot in the function.
I also defined some more variables and plotted different things, but none of that is inside the function, so I don't think that is the issue.
Any help is appreciated, thank you.
0 Comments
Accepted Answer
the cyclist
on 10 Mar 2021
Edited: the cyclist
on 10 Mar 2021
Functions need to be defined in their own files. They cannot be specified in the command window or in scripts.
Put the function definition portion:
function soln = DynEqn1(t,y,I11,I22,I33,Mx,My,Mz)
...
end
into a file named DynEqn1.m.
There may be other errors, but that is the specific problem that is giving that error message.
1 Comment
Stephen23
on 10 Mar 2021
" They cannot be specified ... in scripts."
More Answers (0)
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!