フィルターのクリア

My function wont work?

1 回表示 (過去 30 日間)
Manopka
Manopka 2013 年 5 月 18 日
Whats wrong with this function? it keep ssayinf variables are undefined.
% thsi is the function I wrote
function [maxheight, time, speed] = cannon(x0, y0, v0,angle)
maxheight=(v0^2*sin(angle)^2)/(2*9.8)
time=v0*sin(angle)/9.8
speed=cos(angle)*v0
endfunction
%this is the code given by the professor
x0 = 0;
y0 = 0;
v0 = 20; % initial speed in m/s
angle = 45; % elevation angle in degrees
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f \n' , speed)

回答 (3 件)

Image Analyst
Image Analyst 2013 年 5 月 18 日
Get rid of "endfunction" and it will run fine. Copy this (both functions) into a file called test.m and run it:
function test
%this is the code given by the professor
x0 = 0;
y0 = 0;
v0 = 20; % initial speed in m/s
angle = 45; % elevation angle in degrees
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f \n' , speed)
function [maxheight, time, speed] = cannon(x0, y0, v0,angle)
maxheight=(v0^2*sin(angle)^2)/(2*9.8)
time=v0*sin(angle)/9.8
speed=cos(angle)*v0
It will run fine.
  1 件のコメント
Image Analyst
Image Analyst 2013 年 5 月 18 日
So did you try it and verify that this works?

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


sami
sami 2013 年 5 月 18 日
1) Open matlab-> create new m-file
copy-paste this : function [maxheight, time, speed] = cannon(x0, y0, v0,angle)
maxheight=(v0^2*sin(angle)^2)/(2*9.8);
time=v0*sin(angle)/9.8;
speed=cos(angle)*v0;
end
Ctrl+S (to save it) save it as it is (autodetection of the name of the function is the same as the name of the file.
2) create another new m-file :
copy paste this :
clear all;clc
%this is the code given by the professor x0 = 0; y0 = 0; v0 = 20; % initial speed in m/s angle = 45; % elevation angle in degrees [maxheight,time,speed] = cannon(x0,y0,v0,angle); fprintf('The max height is %7.2f \n' , maxheight) fprintf('The time of the max height is %7.2f seconds \n' , time) fprintf('The speed at the max height is %7.2f \n' , speed)
Ctrl+S (give it a name)
Then run it.
RESULT
The max height is 14.78 The time of the max height is 1.74 seconds The speed at the max height is 10.51
END OF RESULT
  2 件のコメント
Image Analyst
Image Analyst 2013 年 5 月 18 日
Manopka
Manopka 2013 年 5 月 18 日
thank you. this is what i was doing wrong.
the professor told us to paste his code into our file but thats only for when we turn it in.

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


per isakson
per isakson 2013 年 5 月 18 日
"it keep ssayinf variables are undefined". What is the exact error message?
I cannot guess what you are doing to produce an error
Running this script
%%this is the code given by the professor
x0 = 0;
y0 = 0;
v0 = 20; % initial speed in m/s
angle = 45; % elevation angle in degrees
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f \n' , speed)
prints the following in the command window
The max height is 14.78
The time of the max height is 1.74 seconds
The speed at the max height is 10.51
where
function [ maxheight, time, speed] = cannon( x0, y0, v0, angle )
maxheight=( v0^2*sin(angle)^2)/(2*9.8);
time=v0*sin(angle)/9.8;
speed=cos(angle)*v0;
end
The input arguments, x0 and y0, are not used.

カテゴリ

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