Code about paper airplane simulation cannot be run

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 件のコメント

Steven Lord
Steven Lord 2019 年 12 月 20 日
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 2019 年 12 月 21 日
I am using R2019a version of matlab and this is the error I received.
79869318_506490076742227_3268526616845746176_n.jpg

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 21 日

0 投票

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 件のコメント

Lau Pei Shan
Lau Pei Shan 2019 年 12 月 22 日
The error still occured after I changed the name of file.
Screenshot (42).png
Image Analyst
Image Analyst 2019 年 12 月 22 日
Those are just warnings. You can ignore them. But we wonder why you're passing in "t" if you're not going to use it at all? Any idea?
But you need to actually CALL the PaperHoriz() function in the script lines of code above it. Where did you actually call the function? Does ode45 do that? I'm not familiar with that function, so you may be okay. Have you tried running it despite the warnings? What happened?
Walter Roberson
Walter Roberson 2019 年 12 月 23 日
There are only two circumstances under which you can use a character vector representing a function name in a call to the ode*() functions:
  1. Very old versions of MATLAB, when the function was defined inside the same file as the call. I am not sure exactly when this stopped working, but I believe it was before R2006a. Certainly it stopped working long before R2016b, the first release that permitted functions to be defined inside of scripts
  2. In more modern MATLAB, only when the function being defined is the first function defined in a .m file, or is a static method of a class that is on the path, or is a Simulink model name
In particular, when you attempt to use a character vector to refer to a function defined inside the same script, it cannot work in any modern MATLAB.
You need to switch from ode45('PaperHoriz', tspan, s0) to ode45(@PaperHoriz, tspan, s0)
You will, by the way, promptly get an error about unequal number of variables on the two sides of an expression. You will get that because you use undefined global variables. Using global variables leads to failures more often than not. See http://www.mathworks.com/help/matlab/math/parameterizing-functions.html and http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Lau Pei Shan
Lau Pei Shan 2019 年 12 月 24 日
I have corrected the error and finally there are no more error pop out, but here comes another problem, a graph supposed to be plotted out but it did not after I run the programme. Here is the latest coding.
%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(s)
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
Walter Roberson
Walter Roberson 2019 年 12 月 24 日
Change
function sdot = PaperHoriz(s)
to
function sdot = PaperHoriz(~,s)
Lau Pei Shan
Lau Pei Shan 2019 年 12 月 24 日
My programme worked well finally. I am very thankful and appreciated with your guidances. Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by