When coding of function that shows error in yellow underline.
Afficher commentaires plus anciens
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 commentaires
Rik
le 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.
Rena Berman
le 15 Nov 2022
(Answers Dev) Restored edit
Réponses (3)
Star Strider
le 2 Nov 2022
0 votes
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 commentaires
Did you define x1 and x2 somewhere in your code and gave numerical values to them ? I guess no because you get the error message.
Maybe you can describe what you want to compare in the line
if fc > f
Star Strider
le 2 Nov 2022
What I wrote is an illustration of how to compare the functions. The may be more to the comparison as well, for example using the all or any functions to understand how best to evaluate the comparison.
The variables being evaluated must appear in the code before the function is called. I have no idea what variables you are evaluating, since only a part of the code is visible, and there is no mention of the variables being evaluated elsewhere in it.
That is the best I can do with the information I have.
Star Strider
le 2 Nov 2022
Please describe what you want to do, and provide an example of the values you want to use with the functions.
Star Strider
le 2 Nov 2022
Show whatever is necessary for us to understand (and ideally solve) the problem.
Star Strider
le 3 Nov 2022
We still have no idea what the values are for the ‘x’ vector or whatever are supposed to be passed to the functions for ‘x1’ and ‘x2’.
More to the point, what problem are you solving with your code?
Star Strider
le 3 Nov 2022
What doess the code do?
What are the data that are being analysed with it?
We rehally need to know the problem being solved and the data being used.
Walter Roberson
le 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
le 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?
If f is a function handle, this line of your code doesn't make sense:
if fc > f
Only the result of function handles applied to input values of x1 and x2 can be compared, not function handles on their own.
Walter Roberson
le 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??
Catégories
En savoir plus sur Linear Least Squares dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!