필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Travis Schauer
Travis Schauer 2018년 2월 21일
댓글: Travis Schauer 2018년 2월 21일
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;

채택된 답변

Birdman
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
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;

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by