Why am I getting errors with this code?

1 回表示 (過去 30 日間)
J
J 2015 年 2 月 22 日
コメント済み: J 2015 年 2 月 22 日
Could someone help me figure out why I am getting all kinds of errors with my code please. I have attached the *.m file containing my code. Thank you very much.
  3 件のコメント
Andrew Newell
Andrew Newell 2015 年 2 月 22 日
The script at the top doesn't call any of the functions.
J
J 2015 年 2 月 22 日
No, they are all in the same file.

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

採用された回答

Greig
Greig 2015 年 2 月 22 日
編集済み: Greig 2015 年 2 月 22 日
There are a few issues here. First of all, you cannot declare functions in a standard m-file script. They have to be in separate m-files, or trajectory.m needs to be a function.
I recommend renaming it to "Get_trajectory.m" and making the start and end
function Stats = Get_trajectory
.... % all the other functions
end % this is needed since all other function use end
Second, by defining trajectory as a function, we need to declare what the output should be. I guess it is all the output from your various functions? If so, then we need to call your various functions get their output and return this as the output of the Get_trajectory function (the "Stats" variable above). So, add something like this after you ask for the user input...
[xt,yt] = trajectory(v0,theta,t,g);
ymax = peakheight(v0,theta,g);
tmax = timeflight(v0,theta,g);
xmax = range(v0,theta,g);
Stats = [xt, yt, ymax, tmax, xmax];
Third, in the peakheight function, you a missing a * in the ymax calculation
function [ymax] = peakheight(v0,theta,varargin)
if nargin == 2
g=9.8; % if g not specified g=9.8
ymax = ((v0^2)*((sin(theta))^2))/2*g;
elseif nargin == 3
%if the user enter the value for g
g=varargin{1};
ymax = ((v0^2)*((sin(theta))^2))/2*g;
end
end
Lastly, to call the function simply type
Stats = Get_trajectory;
Type in the prompted inputs, and the stats variable will contain all of the output you need. I have attached my updated version for you to see.
Edit: The file is now attached!
  1 件のコメント
J
J 2015 年 2 月 22 日
Greig, I really appreciate your help. Thank you very much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by