MATLAB 'Error using plot Vectors must be the s

2019-03-04 03:00发布

问题:

x = 0:1:5; % define x array 
y = exp(x)+x.^4+2*x.^2-x+3; % define y array
dx = diff(x); % 1 1 1 1 1 dx has one number less than x 
dy = diff(y); % -5 -1 -3 -4 -2
slope = dy./dx
z=exp(x)+4*x.^3 +4*x-1
plot(x,slope,'*',x,z)

'Error using plot Vectors must be the same length.'

Been stuck on this for a while, I am not sure what to do. ive seen the other responses to the same error but can not understand the code. It seems its the colon : that might help but i am not sure how to use it.

Any help would be appreciated!

回答1:

Since slope is computed from two consecutive values, I suggest you take the average betwen consecutive points as the x-axis values for ´slope`:

plot((x(1:end-1)+x(2:end))/2,slope, '*',x,z)



标签: matlab vector