Error: Array indices must be positive integers or logical values.

17 views (last 30 days)
Here is my code:
clc;clear;
t=[0:0.5:60];
h(t)=2.03*t.^2-0.0013*t.^4+0.000034*t.^4.751;
plot(t, h(t));
xlabel('Time');
ylabel('Height(m)');
But I get this error when I try to evaluate:
Array indices must be positive integers or logical values.
I need 100 values from 0 to 60 for my graph.
Thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2021

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 28 Feb 2021
Matlab has the convention that arrays-indexes starts from 1. Arrays have to be indexed with integers.
You can solve your problem in 2 ways. Either you simply assign the values you want directly to the array:
h = 2.03*t.^2 - 0.0013*t.^4 + 0.000034*t.^4.751;
or you define a function for h:
h = @(t) 2.03*t.^2 - 0.0013*t.^4 + 0.000034*t.^4.751;
In the first case you simply plot the 2 arrays:
plot(t,h)
In the second case you plot the function:
plot(t,h(t))
It seems you're very new to matlab - therefore I recommend you to look through the on-ramp material to get up to speed as fast as possible.
HTH

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!