Borrar filtros
Borrar filtros

Can't figure out how to fix integer/logical error

2 visualizaciones (últimos 30 días)
Travis Schauer
Travis Schauer el 21 de Feb. de 2018
Comentada: Travis Schauer el 21 de Feb. de 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;

Respuesta aceptada

Birdman
Birdman el 21 de Feb. de 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')

Más respuestas (1)

Aletta Wilbrink
Aletta Wilbrink el 21 de Feb. de 2018
Editada: Aletta Wilbrink el 21 de Feb. de 2018
The problem is t. h(t) asks for integers.
It does work if you do:
t = 1:30;

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by