Variable not recognized after program restart

2 views (last 30 days)
So I've been writing some code for a university project, and while I was doing the exact same thing every time I opened my project (open it and when i tried to run it, i added it to the default matlab path). for some reason I get this error:
Undefined function or variable 'x'.
Error in Ex1 (line 4)
df = matlabFunction( diff(f, x) );
My code is this on the first lines:
%Setting of initial functions and variables. Plotting of our function.
h = 10^-6;
f = @(x) 14*x.*exp(x-2) - 12*exp(x-2) - 7*x.^3 + 20*x.^2 - 26*x + 12;
df = matlabFunction( diff(f, x) );
ddf = matlabFunction( diff(df, x) );
...
I believe the code is correct. Besides I have run the code multiple times without a problem!
Do you have any ideas?

Accepted Answer

madhan ravi
madhan ravi on 6 Jan 2019
Edited: madhan ravi on 6 Jan 2019
diff recognises symbolic x but not as a function handle
syms x
h = 10^-6;
f = 14*x.*exp(x-2) - 12*exp(x-2) - 7*x.^3 + 20*x.^2 - 26*x + 12;
df = matlabFunction( diff(f, x) );
ddf = matlabFunction( diff(df, x) );
  8 Comments
Fjolfrin
Fjolfrin on 6 Jan 2019
It does work!
I have some rutnrime problems down the line with some of my functions, but that is another problem, I must have an infinite loop somewhere.
Thanks a lot!
BTW I just realised I've been reading your comments on many occations while I was browsing for various answers for problems I had. Additional thanks for those tips! Keep up!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!