Variable not recognized after program restart
显示 更早的评论
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?
采纳的回答
madhan ravi
2019-1-6
编辑:madhan ravi
2019-1-6
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 个评论
I get this error when I make this change:
Index exceeds array bounds.
Error in sym/subsref (line 859)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in Ex1 (line 18)
while abs(f(Bisection)) > h
I'm pretty sure that's not it. As I have told in my initial question, this code WORKS. I have seen it working perfectly fine. I believe the problem lies with what path Matlab accepts, I'm not entirely sure though.
I have seen the method you propose, but it doesn't fullfil my needs for this code.
First of all in your question
while abs(f(Bisection)) > h
The above code didn't even appear in the question you asked and now you add up there's an error here??
Did you try my answer first (read the link) ?? use CLEAR ALL at the very beginning ,
which x -all % what shows up?
which diff -all
Honestly I don't believe that you say it's not working but worked before when I tried your code :
>> 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) );
Undefined function or variable 'x'. % SEE!!
After I implemented your method, I get:
which x -all
%x is a variable
which diff -all
%'All the different paths to a diff function'
Again, based on the provided link, you rpopose I do it like this?:
syms x;
f = 14*x.*exp(x-2) - 12*exp(x-2) - 7*x.^3 + 20*x.^2 - 26*x + 12;
df = matlabFunction( diff(f(x), x) );
ddf = matlabFunction( diff(df(x), x) );
When I do, i get:
Error using sym/subsindex (line 814)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments
must be symbolic variables, and function body must be sym expression.
Error in sym/subsref (line 859)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in Ex1 (line 5)
df = matlabFunction( diff(f(x), x) );
If I ommit the '(x)' on the diff decleration (meaning I type "df = matlabFunction( diff(f, x) );")I have another problem further down the line:
Index exceeds array bounds.
Error in sym/subsref (line 859)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in Ex1 (line 18)
while abs(f(Bisection)) > h
The code the compiler refers to is:
tic;
l = lower; Bisection = m; u = upper; noBi = 1;
while abs(f(Bisection)) > h
if f(Bisection)*f(l) < 0
temp = u;
u = Bisection;
elseif f(Bisection)*f(u) < 0
temp = l;
l = Bisection;
end
Bisection = (l + u) / 2.0;
aer = (Bisection - temp);
noBi = noBi + 1;
end
toc;
BiTime = toc;
Hey ! where did I use
df = matlabFunction( diff(f(x), x) );
^^^---- is it there in my answer!!!??
You have some problem with copying the code properly.
If you my try answer properly you will get:
>> 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) )
df =
function_handle with value:
@(x)x.*4.0e1+exp(x-2.0).*2.0+x.*exp(x-2.0).*1.4e1-x.^2.*2.1e1-2.6e1
ddf =
function_handle with value:
@(x)x.*-4.2e1+exp(x-2.0).*1.6e1+x.*exp(x-2.0).*1.4e1+4.0e1
>>
It was in the link you provided:

Anyway I have enough material up to now.
Thanks for the advice, I'll figure it out. It seems I have many things mixed up.
Ah I shared the link of sir Walter's answer because he has beautifully explained how to use diff() (link - many examples available there).
By the way just try the below alternative (if it works make sure to accept the answer) :
Code:
clear all
syms x
h = 10^-6;
f(x) = 14*x*exp(x-2) - 12*exp(x-2) - 7*x^3 + 20*x^2 - 26*x + 12;
df(x) = ( diff(f(x), x) )
ddf(x) = ( diff(df(x), x) )
Gives:
df(x) =
40*x + 2*exp(x - 2) + 14*x*exp(x - 2) - 21*x^2 - 26
ddf(x) =
16*exp(x - 2) - 42*x + 14*x*exp(x - 2) + 40
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!
Anytime :) ,
Thank you
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Code Performance 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
