Code about paper airplane simulation cannot be run

1 Ansicht (letzte 30 Tage)
Lau Pei Shan
Lau Pei Shan am 20 Dez. 2019
Kommentiert: Lau Pei Shan am 24 Dez. 2019
I am not that proficient in programming. The error occured on the line "function sdot = PaperHoriz(t,s)", and I am not sure how to solve this error. The name of this M file is "PaperHoriz.m".
%paper airplane simulation
global CL CD S m g rho
S = 0.017;
m = 0.003;
g = 9.81;
rho = 1.225;
CD = 0.04; %lift and drag coeff simply given
CL = 0.22; %for this flight regime
gamma0 = -atan(CD/CL); %-ve sign because decent angle
v0 = sqrt(2*m*g/(rho*S*sqrt(CL^2+CD^2))); %initial velocity for min descent angle
%various initial flight condition
H = 2;
R = 0;
t0 = 0;
tf = 6;
tspan = [t0 tf];
%a) trimmed flight case
%initial angle is min angle of descent
s0 = [H;R;v0;gamma0]; %state vector's initial state
[ta, sa] = ode23('PaperHoriz',tspan,s0);
%b) same initial velocity but release at horizontal angle
gamma0 = 0;
s0 = [H;R;v0;gamma0];
[tb, sb] = ode23('PaperHoriz',tspan,s0);
%c) release at horizontal angle, double min-descent speed
newv0 = 2*v0;
s0 = [H;R;newv0;gamma0];
[tc, sc] = ode45('PaperHoriz',tspan,s0);
%d) release at horizontal angle, triple min-descent speed
newv0 = 3*v0;
s0 = [H;R;newv0;gamma0];
[td, sd] = ode45('PaperHoriz',tspan,s0);
%plot all the results
plot(sa(:,2),sa(:,1),sb(:,2),sb(:,1),sc(:,2),sc(:,1),sd(:,2),sd(:,1))
grid on;
ylim([0 5]);
xlim([-0.5 18]);
%invoked file with dynamics in it called PaperHoriz.m
function sdot = PaperHoriz(t,s) %error occured in this line
global CL CD S m g rho
v = s(3);
gamma = s(4);
q = rho/2*v^2;
%state vector = [H;R;v;gamma]
sdot = [v*sin(gamma);v*cos(gamma);1/m*(-q*S*CD-m*g*sin(gamma));1/(m*v)*(q*S*CL-m*g*cos(gamma))];
end
  2 Kommentare
Steven Lord
Steven Lord am 20 Dez. 2019
What is the full and exact text of the error message you receive (all the text displayed in red and/or orange, exactly as it's displayed in the Command Window)?
Which release of MATLAB are you using?
Lau Pei Shan
Lau Pei Shan am 21 Dez. 2019
I am using R2019a version of matlab and this is the error I received.
79869318_506490076742227_3268526616845746176_n.jpg

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Dez. 2019
The name of this M file is "PaperHoriz.m".
Name the file something else, like PaperHorizDriver.m .
When you have a script that defines a function, the script name cannot be the same as the name of the function you are defining.
  6 Kommentare
Walter Roberson
Walter Roberson am 24 Dez. 2019
Change
function sdot = PaperHoriz(s)
to
function sdot = PaperHoriz(~,s)
Lau Pei Shan
Lau Pei Shan am 24 Dez. 2019
My programme worked well finally. I am very thankful and appreciated with your guidances. Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by