differentiating function & getting different answer
Mostra commenti meno recenti
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 Commenti
Aquatris
il 7 Feb 2019
What version of Matlab are you using? I typed the exact code in Matlab 2013a and 2017a and it gave me the correct answer.
James Tursa
il 7 Feb 2019
What does this show:
which diff
Noah Barrow
il 7 Feb 2019
Noah Barrow
il 7 Feb 2019
Risposte (2)
James Tursa
il 7 Feb 2019
Modificato: James Tursa
il 7 Feb 2019
0 voti
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
il 7 Feb 2019
0 voti
4 Commenti
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)
Noah Barrow
il 8 Feb 2019
Walter Roberson
il 8 Feb 2019
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.
Noah Barrow
il 8 Feb 2019
Categorie
Scopri di più su Startup and Shutdown in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!