When coding of function that shows error in yellow underline.
1 view (last 30 days)
Show older comments
Function Code:
f=@(x1,x2) ((x1).^2+2*(x2).^2-4*(x1)-2*(x1.*x2));
In this function yellow underline shows below 'f' and occurs error.
Error:
*Value assigned to variable does not used
Code:
While running followoing code, which showes error:
Undefined function 'gt' for input arguments of type 'function_handle'.
Error in untitled (line 36)
if fc > f
f=0;
% After 1st iteration
while norm(nc) > eps
x=x+alp*nc;
f=fc; gf=gfc; n=nc; H=Hc;
fc=@(x1,x2) ((x1).^2+2*(x2).^2-4*(x1)-2*(x1.*x2));
gfc=[2*x(1)-4-2*x(1) 4*x(2)-2*x(1)]';
D=(gfc-gf)*(gfc-gf)'./((gfc-gf)'*(alp*n));
E=gf*gf'./(gf'*n);
Hc=H+D+E;
nc=-inv(Hc)*gfc;
%nc=nc./norm(nc);removed
if fc > f
alp=alp/2;
end
2 Comments
Rik
on 3 Nov 2022
I recovered some of the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Answers (3)
Star Strider
on 2 Nov 2022
I have no idea what ‘f’ is, however in order to compare function outputs, it is necessary to evaluate the functions.
So the correct approach would be:
if fc(x1,x2) > f(x1,x2)
assuming both are defined as such.
.
8 Comments
Walter Roberson
on 3 Nov 2022
There are a quite small number of operations permitted on function handles:
- you can disp() or display() the handle to observe it
- you can use nargin() and nargout() to ask how many input arguments and how many output arguments it expects
- you can use func2str() to create a text representation of the function handle
- you can use functions to ask for some internal information about the function handle that is sometimes useful
- you can use standard functions such as size() and length() on the function handle. However function handles are always scalar, always 1 x 1
- you can assign the function handle to a location or pass the function handle as a parameter to a function. If the location uses () indexing as the last component, then the index must be 1. For example, h(1)=@sign is permitted but not h(2)=@sign
- you can store the function handle as any member of a cell array. Which is really just the same point as above, but is worth emphasizing because you can use, for example, h{2}=@sign if you want to create an array of function handles
- you can invoke the function handle with parameters in order to run the function indicated. There must be an () after the function handle in order to invoke it; in any situation in which there is no () after the function handle, you are copying the handle instead of invoking it. (Exception: feval() can invoke a function handle using a syntax that does not have () after the handle.)
If you have a function handle and you do not have () after it, such as if you have if fc > f and fc is a function handle, then you are not executing the function, and you will have problems.
Rik
on 3 Nov 2022
Unrecognized function or variable 'x1'.
Error in untitled (line 36)
if fc(x1,x2) > f(x1,x2)
*After your advice I used that code but that shows above error. Can you help?
[2 comments from Torsten and Star Strider]
Whole code ready just showing this minor error. Since 2 days I am suffering with this issue. Can I show you section of code where I am facing error? And I didn't apply any value of x1 & x2, just mention function and derived constraint.
[1 comment from Star Strider]
Can I show you the function and code?
Walter Roberson
on 2 Nov 2022
f=0;
% After 1st iteration
while norm(nc) > eps
x=x+alp*nc;
f=fc; gf=gfc; n=nc; H=Hc;
fc=@(x1,x2) ((x1).^2+2*(x2).^2-4*(x1)-2*(x1.*x2));
gfc=[2*x(1)-4-2*x(1) 4*x(2)-2*x(1)]';
D=(gfc-gf)*(gfc-gf)'./((gfc-gf)'*(alp*n));
E=gf*gf'./(gf'*n);
Hc=H+D+E;
nc=-inv(Hc)*gfc;
%nc=nc./norm(nc);removed
if fc > f
alp=alp/2;
end
end
You test fc against f inside the while loop.
Do you change fc inside the loop? You do change fc inside the loop, but you assign it the exact same thing each time, the handle to an anonymous function, so at most it changes between the first time the loop iterates (where it it copied to f) versus the rest of the iterations (where it will always be the same.)
Do you change f inside the loop? You do change f inside the loop, but you assign it the current value of fc just before you overwrite fc with the (same) anonymous function each time, so at most it is different on the very first iteration of the loop; after that it will be what fc was assigned in the previous iteration, which is the same anonymous function each time.
What is fc before the while loop? We do not know.
You have f=0 before the while loop, but you overwrite f with the content of fc early in the loop, so the value of f when you enter the while loop does not matter; it will only be important what fc is the first time through.
You appear to be expecting to be able to compare fc to f, and your assignment of 0 to f hints that you expect fc and f to be numeric at the point of the test. That hints that maybe fc is numeric when you start the loop, perhaps. But you assigned an anonmous function to fc so at best the first time through you might perhaps be trying to compare the anonymous function itself to a numeric value; after that you would be trying to compare the anonymous function to the exact same anonymous function created in the previous iteration. It is not clear what it would mean to compare two anonymous functions if it were permitted -- compare the storage addresses maybe??
0 Comments
See Also
Categories
Find more on Linear Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!