Can't figure out how to fix integer/logical error
3 次查看(过去 30 天)
显示 更早的评论
I am writing a script to plot the trajectory of a ball. I can't quite figure out how to fix an error I have. I was thinking about using a loop, but I am not familiar enough with loops to know what to do.
Here is my script:
% Creates plot of ball based on height and velocity
% Gathers height and velocity from user
h = input('How high is the ball in meters? ');
v = input('What is the speed of the ball in m/s? ');
% Data for time
t = linspace(0,30,200);
% Calculates Height
h(t) = (.5)*(-9.81)*power(t,2)+(v*t)+h;
% Calculates Velocity
v(t) = ((-9.81)*t)+v;
% Plots Data
hold on
title ('Height and Velocity of a Ball')
xlabel ('Velocity (v) [m/s]')
ylabel ('Height (h) [m]')
plot (t,h(t),'r*')
plot (t,v(t),'bo')
legend ('Height', 'Velocity','location','northwest')
Here is the error I get
Subscript indices must either be real positive integers or logicals.
Error in HW5_Prob_1_2_3 (line 39)
h(t) = (.5)*(-9.81)*power(t,2)+(v*t)+h;
0 个评论
采纳的回答
Birdman
2018-2-21
% Creates plot of ball based on height and velocity
% Gathers height and velocity from user
h = input('How high is the ball in meters? ');
v = input('What is the speed of the ball in m/s? ');
% Data for time
t = linspace(0,30,200);
% Calculates Height
h = (.5).*(-9.81).*power(t,2)+(v.*t)+h;
% Calculates Velocity
v = ((-9.81).*t)+v;
% Plots Data
hold on
title ('Height and Velocity of a Ball')
xlabel ('Velocity (v) [m/s]')
ylabel ('Height (h) [m]')
plot (t,h,'r*')
plot (t,v,'bo')
legend ('Height', 'Velocity','location','northwest')
更多回答(1 个)
Aletta Wilbrink
2018-2-21
编辑:Aletta Wilbrink
2018-2-21
The problem is t. h(t) asks for integers.
It does work if you do:
t = 1:30;
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!