MATLAB: How can i calculate the derivative of an x vector

derivativehomework

Starts with an x vector created using linspace with a starting point of -5 and ends at +4 and at least 50 data points.
Evaluate the function f = 2×4 – 5×3 -12×2 + 30x +10 as a function of the MATLAB vector By hand, calculate the first derivative and then evaluate using the same x-vector. By hand, calculate the second derivative and then evaluate using the same x-vector.
Plot the three equations using different line color and different markers. You may use the extended plot command or roots.
Add a graph title, axes titles, and a legend.

Best Answer

Try the below:
syms f(x)
f = 2*x.^4 - 5*x.^3 -12*x.^2 + 30*x +10
first_derivative = diff(f)
second_derivative = diff(f,2)
%solved symbolically till here
%using numerical method from the above
x=linspace(-5,4,50)
subplot(3,1,1)
plot(subs(x),subs(first_derivative),'*r-')
xlabel('x')
ylabel('first derivative')
title('1ST DERIVATIVE')
legend('FIRST DERIVATIVE')
subplot(3,1,2)
plot(subs(x),subs(second_derivative),'og-')
xlabel('x')
ylabel('second derivative')
title('2ND DERIVATIVE')
legend('SECOND DERIVATIVE')
subplot(3,1,3)
plot(subs(x),subs(f),'sqb-')
xlabel('x')
ylabel('equation')
title('FUNCTION')
legend('FUNCTION')
Start getting familiar with the Matlab functions soon , the sooner the better.
Related Question