differentiating function & getting different answer

hey! I was trying to differentiate this function g(x)=3x-1 and despite the fact that I followed the code provided on the matlab website to a "t", the answer I got within matlab was incorrect. I included a link to the example as well as screenshots of the code they provided and how it appears in my command window.
here's how the code appears on the site:
I typed the same code into my command window but got a different answer...
I'm really confused at this point because now whenever I try to differentiate ANY function I get the same answer!

댓글 수: 4

What version of Matlab are you using? I typed the exact code in Matlab 2013a and 2017a and it gave me the correct answer.
What does this show:
which diff
when I type this in it says "diff is a variable"
i'm using 2018b. is the code drastically different?

댓글을 달려면 로그인하십시오.

답변 (2개)

James Tursa
James Tursa 2019년 2월 7일
편집: James Tursa 2019년 2월 7일

0 개 추천

Perhaps you are shadowing the MATLAB function diff with a function of your own. Make sure diff is pointing to the MATLAB function.
Noah Barrow
Noah Barrow 2019년 2월 7일

0 개 추천

can you explain what shadowing is? The code I entered in my command window is directly above-does it appear that my "diff" is pointing to the matlab function?

댓글 수: 4

Aquatris
Aquatris 2019년 2월 7일
편집: Aquatris 2019년 2월 8일
Shadowing means that your workspace has a variable called "diff" so when you run the command "diff(g)", instead of going to the built-in diff() function of Matlab, it tries to run the "diff" in the workspace (or a function in your working directory). That is why it is common practice to not name variable or custom functions with the same name as Matlab's built-in ones.
When I run which diff I get;
which diff
built-in (C:\Program Files\MATLAB\R2013a\toolbox\matlab\datafun\diff)
ah ha! okay, because when I type in diff it says "diff is a variable". So how should i proceed? Am I simply using the wrong command?
Execute the command
clearvars
and then run your code again. If the same thing happens, then track down where in your code you assign something to a variable named diff and use a different variable name instead.
FIXED!
Thank you so much :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 2월 7일

댓글:

2019년 2월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by