Can't figure out how to fix integer/logical error
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Travis Schauer
am 21 Feb. 2018
Kommentiert: Travis Schauer
am 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 Kommentare
Akzeptierte Antwort
Birdman
am 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')
Weitere Antworten (1)
Aletta Wilbrink
am 21 Feb. 2018
Bearbeitet: Aletta Wilbrink
am 21 Feb. 2018
The problem is t. h(t) asks for integers.
It does work if you do:
t = 1:30;
0 Kommentare
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!