why does it tell me "index exceeds matrix dimensions"??

3 views (last 30 days)
I'm trying to run the following code:
x = 10:30; y = exp((-2139.2) ./ (x - 34.42));
A = [x ; y(x)]
plot(x,y,'b','lineWidth',2.5); grid; xlabel('temperature in degree celcius','Fontsize',16); ylabel('vapor pressure','Fontsize',16)
But when I try, it tells me that index exceeds matrix dimensions.
Can somebody help?
  1 Comment
Jacky Jo
Jacky Jo on 21 Sep 2015
X has 21 values.
Y has the same.
but when we do operation y(x) =y(20:30),
we are trying to call values from 20 to 30 in y which is impossible since y just has values from 1 to 21.
So you have to change the code for y(i) where i could be only vary from 1 to 21.
ex:
A = [x ; y(1:21)];

Sign in to comment.

Answers (2)

Kirby Fears
Kirby Fears on 21 Sep 2015
You do not need to write y(x). You have already defined y, so just call it y. Code fixed below:
x = 10:30;
y = exp((-2139.2) ./ (x - 34.42));
A = [x ; y];
plot(x,y,'b','lineWidth',2.5);
grid;
xlabel('temperature in degree celcius','Fontsize',16);
ylabel('vapor pressure','Fontsize',16)

Star Strider
Star Strider on 21 Sep 2015
you defined ‘y’ as an array, not a function, so you cannot call it as a function.
This works:
A = [x ; y];

Community Treasure Hunt

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

Start Hunting!