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.
madhan ravi
2019년 1월 6일
편집: madhan ravi
2019년 1월 6일
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;
madhan ravi
2019년 1월 6일
편집: madhan ravi
2019년 1월 6일
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.
madhan ravi
2019년 1월 6일
편집: madhan ravi
2019년 1월 6일
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)
