Can't figure out how to fix integer/logical error
3 views (last 30 days)
Show older comments
Travis Schauer
on 21 Feb 2018
Commented: Travis Schauer
on 21 Feb 2018
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 Comments
Accepted Answer
Birdman
on 21 Feb 2018
% 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')
More Answers (1)
Aletta Wilbrink
on 21 Feb 2018
Edited: Aletta Wilbrink
on 21 Feb 2018
The problem is t. h(t) asks for integers.
It does work if you do:
t = 1:30;
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!