My function wont work?
1 回表示 (過去 30 日間)
古いコメントを表示
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)
0 件のコメント
回答 (3 件)
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.
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 件のコメント
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Denoising and Compression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!