Don't understand the reason this code is giving me errors.

3 views (last 30 days)
The code that is producing the error is below. It says:
>> test
Error: File: test.m Line: 32 Column: 58
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
>> test
Error: File: test.m Line: 30 Column: 60
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Code that produces errors is below.
clc;
mass = 13.5;
Jx = 0.8244;
Jy = 1.135;
Jz = 1.759;
Jxz = 0.1204;
G = Jx*Jz-Jxz^2;
G1 = Jxz*(Jx - Jy + Jz)/G;
G2 = (Jz*(Jz-Jy)+Jxz^2)/G;
G3 = Jz/G;
G4 = Jxz/G;
G5 = (Jz-Jx)/Jy;
G6 = Jxz/Jy;
G7 = ((Jx-Jy)*Jx +Jxz^2)/G;
G8 = (Jx/G);
p = 0;
q = 0;
r = 0;
l = 0.0000;
m = 0.0000;
n = 0.0000;
tspan = [0 10];
%[t,q] = ode45(@(t,q) (G5*p*r-G6*(p^2-r^2)+m/Jy), tspan,0);
%plot(t,q);
%pdot = (G1*p*q-G2*q*r + G3*l+G4*n);
[t,p]=ode45(@(t,p) (G1*p*q-G2*q*r+G3*l+G4*n),[0 10], 0.1);
plot(t,p,'--r');
disp(p);
disp(t);
[t,q] = ode45(@(t,r) (G5*p*r-G6*p*p-G6*r*r)+m/Jy), [0 10],0);
plot(t,q,'--g');
[t,r] = ode45(@(t,r) (G7*p*q-G1*q*r + G4*l+G8*n), [0 10],0);
plot(t,r,'--b')

Accepted Answer

Voss
Voss on 22 Aug 2024
You have an extra ")" here:
[t,q] = ode45(@(t,r) (G5*p*r-G6*p*p-G6*r*r)+m/Jy), [0 10],0);
% ^ extra parenthesis
That parenthesis closes the ode45 function call, leaving the rest of the line ", [0 10],0);" hanging out with nothing to do, and the closing parenthesis in that part causes the syntax error.
  2 Comments
DJ V
DJ V on 22 Aug 2024
Now it gives me the following error:
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0.1000
0
0.2500
0.5000
0.7500
1.0000
1.2500
1.5000
1.7500
2.0000
2.2500
2.5000
2.7500
3.0000
3.2500
3.5000
3.7500
4.0000
4.2500
4.5000
4.7500
5.0000
5.2500
5.5000
5.7500
6.0000
6.2500
6.5000
6.7500
7.0000
7.2500
7.5000
7.7500
8.0000
8.2500
8.5000
8.7500
9.0000
9.2500
9.5000
9.7500
10.0000
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To
perform elementwise multiplication, use '.*'.
Error in test>@(t,r)(G5*p*r-G6*p*p-G6*r*r+m/Jy) (line 30)
[t,q] = ode45(@(t,r) (G5*p*r - G6*p*p - G6*r*r + m/Jy), [0 10],0);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in test (line 30)
[t,q] = ode45(@(t,r) (G5*p*r - G6*p*p - G6*r*r + m/Jy), [0 10],0);
Voss
Voss on 22 Aug 2024
If the original question is solved, please "Accept" this answer. Thanks!
Feel free to post a new question about error(s) you get running ode45.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!