Problem with derivative function syntax

2 views (last 30 days)
Nathaniel
Nathaniel on 21 Oct 2014
Answered: Star Strider on 21 Oct 2014
Ok so basically what I'm doing here is trying to graph a function and its derivative in matlab. This is what I have:
clear; clc; close all; x=(-2:0.1:4); y=x.^3-2*x.^2-10*sind(x).^2-exp(0.9*x); yd=3*x.^2-4*x-20*sind(x)*cosd(x)-0.9*exp(0.9*x); plot(x,y,x,yd,'--'); xlabel('X Values'); ylabel('Y Values'); legend('function', 'first derivative')
There are no errors in my original function. But for some reason, the derivative function keeps giving me an error. It says "Error using * Inner matrix dimensions must agree. If anyone can help me fix the syntax, it would be greatly appreciated!

Answers (1)

Star Strider
Star Strider on 21 Oct 2014
Since sind and cosd are both functions of row vector ‘x’, they produce row vectors as output. To multiply them, you need to vectorise them, replacing (*) with (.*):
yd=3*x.^2-4*x-20*sind(x).*cosd(x)-0.9*exp(0.9*x);
With that change, it works!

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!