CODE

Hi, I have the following code but when i run it gives an error . The code :
function lambda = drive_zr17t9001
format short e;
Params = load('saved_data.mat');
theta = pi/2;
zeta = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';
%T is a matrix(5,5)
Mroot = Params.M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E =real( eig(T));
%find the negative eigen values -- %find the smallest negative eigen value
gamma = min(E);
%now solve for lambda
bounds = sort([0, -1/gamma]); %in case gamma is positive
M_inv = inv(Params.M); %optimization
lambda = fzero(@(lambda) zr17t9001(lambda,M_inv,Q,Params.zm),bounds); %do the searching
% end
function r = zr17t9001(lambda, M_inv, Q, zm)
Winv = inv(M_inv+lambda.*Q);
r = -(Params.zm)'*M_inv*Winv*Q*Winv*M_inv*(Params.zm);
%end
The error
??? Error using ==> fzero
FZERO cannot continue because user supplied function_handle ==> @(lambda) zr17t9001(lambda, M_inv, Q, Params.zm)
failed with the error below.
Undefined variable "Params" or class "Params.zm".
Error in ==> drive_zr17t9001 at 19
lambda = fzero(@(lambda) zr17t9001(lambda, M_inv, Q, Params.zm), bounds); %do the searching

 Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Nov. de 2011

0 votos

[Edited to define the anonymous function as a separate step]
function lambda = drive_zr17t9001
format short e;
Params = load('saved_data.mat');
theta = pi/2;
zeta = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';
%T is a matrix(5,5)
Mroot = Params.M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E =real( eig(T));
%find the negative eigen values -- %find the smallest negative eigen value
gamma = min(E);
%now solve for lambda
bounds = sort([0, -1/gamma]); %in case gamma is positive
M_inv = inv(Params.M); %optimization
zm = Params.zm;
Lz = @(lambda) zr17t9001(lambda,M_inv,Q,zm);
lambda = fzero(Lz,bounds); %do the searching
% end
function r = zr17t9001(lambda, M_inv, Q, zm)
Winv = inv(M_inv+lambda.*Q);
r = -zm'*M_inv*Winv*Q*Winv*M_inv*zm;
%end

15 comentarios

zayed
zayed el 29 de Nov. de 2011
when i evaluate zm = Params.zm ,it gives:
??? Undefined variable "Params" or class "Params.zm".
Also when i evaluate M_inv =inv(Params.zm) ,it gives:
??? Undefined variable "Params" or class "Params.M_inv".
Walter Roberson
Walter Roberson el 29 de Nov. de 2011
How exactly are you doing the evaluation?
Is the Params structure in your workspace? If not then please single-step through the code and check that Params is created by the "load", and check after each statement to see if Params has disappeared.
zayed
zayed el 29 de Nov. de 2011
I highlight which i want to evaluate in the code then right click and choose "evaluate selection".I don't find "Params " in workspace .But i don't understand the next step,please.
zayed
zayed el 29 de Nov. de 2011
I have evaluated each statement ,it works except at line 23 it gives error:
??? Error: "zr17t9001" is not the name of a function nor a variable,
but is used in an anonymous function either at the prompt or in the argument of EVAL.
Walter Roberson
Walter Roberson el 29 de Nov. de 2011
Please use the debugger instead of "evaluate selection". See http://www.mathworks.com/help/techdoc/matlab_env/brqxeeu-175.html
zayed
zayed el 29 de Nov. de 2011
Params disappears when execution of line 23 (fzero),and above
Walter Roberson
Walter Roberson el 29 de Nov. de 2011
We would not expect Params to exist within zr17t9001 as Params is local to the drive_* function workspace.
Params is not needed within zr17t9001 as we have extracted all necessary values from it and we pass them in to fzero by way of the anonymous function.
By stopping at the "zm =" line you should be able to examine the M_inv matrix that you wanted to look at earlier.
At the command line, give the commands
dbstop if error
dbstop if caught error
dbstop if warning
and then run the drive_* routine. If it encounters an error, it should stop at the line that caused the error.
zayed
zayed el 29 de Nov. de 2011
It stopped @ line 23 lambda=
Walter Roberson
Walter Roberson el 29 de Nov. de 2011
What is the error message being shown now?
I have edited the source in my Answer to create the function handle a separate step, to help isolate which part is causing the problem.
zayed
zayed el 30 de Nov. de 2011
The error message now is :
"?? Error using ==> fzero
"Function values at interval endpoints must be finite and real".this error message is a diplay statement in(fzero.m) in matalb,so may be fzero is unsuitable to solve this equation.May be we should use Newton-Raphson method ,but i don't know if this correct.
Error in ==> drive_zr17t9011 at 24
lambda = fzero(Lz,bounds); %do the searching"
Walter Roberson
Walter Roberson el 30 de Nov. de 2011
Well, in one of the previous instances of this thread, I suggested that the function is probably returning a complex value. As you now have some experience with the debugger, set a breakpoint in zr17t9001 and step through it and see what r value comes out.
zayed
zayed el 30 de Nov. de 2011
There are two values of lambda ,when lambda=0,then Q(complex),
when lambda=97.004,then Q(complex)&W(complex).There is no appearance for r in workspace.
Walter Roberson
Walter Roberson el 30 de Nov. de 2011
fzero() cannot deal with functions that have complex results. Perhaps the bound should be adjusted to exclude 0 and -1/gamma themselves ?
Newton Raphson methods can apparently deal with complex values; I cannot tell at the moment whether special coding would be required for the complex case. http://en.wikipedia.org/wiki/Newton%27s_method#Complex_functions
zayed
zayed el 30 de Nov. de 2011
Can you help in Newton Raphson methods in matlab for the function and the same data in the previous code,please .
Walter Roberson
Walter Roberson el 30 de Nov. de 2011
Sorry, I have other things to do. You can find plenty of Newton Raphson implementations around, as the technique is a common course assignment.

Iniciar sesión para comentar.

Más respuestas (1)

Matt Tearle
Matt Tearle el 29 de Nov. de 2011

0 votos

Change the instances of Params.zm in your subfunction zr17t9001 to just zm. (Leave them as they are in the main function drive_...)
Here's why: in the subfunction, you define zm as an input. You are not passing the whole Params structure in, just one particular value that, locally, is called zm.
Note: I copied exactly what you pasted, made the change I suggested (change r = -(Params.zm)'*M_inv*Winv*Q*Winv*M_inv*(Params.zm); to r = -(zm)'*M_inv*Winv*Q*Winv*M_inv*(zm);) and it worked fine for me. Obviously I had to create a saved_data.mat with some fake data in it.

8 comentarios

zayed
zayed el 29 de Nov. de 2011
It gives error at line 23 lambda=fzero
Matt Tearle
Matt Tearle el 29 de Nov. de 2011
Any particular error?
And, just to be clear, you took exactly the code you initially posted, and changed the two instances of Params.zm on the second-to-last line (r = - ...) to zm, right? And no other changes?
zayed
zayed el 29 de Nov. de 2011
The error is
"??? Error using ==> fzero
Function values at interval endpoints must be finite and real.
Error in ==> drive_zr17t90011 at 22
lambda = fzero(@(lambda) zr17t90011(lambda,M_inv,Q,Params.zm),bounds); %do the searching".
I took the initially posted code of mine,and i did the change.
But if look at the error message it seems that fzero may be not suitable to solve this function may be we need NR-method,exactly i do not now.....
Matt Tearle
Matt Tearle el 30 de Nov. de 2011
OK, good. So the posted problem (Undefined variable "Params" or class "Params.zm") is solved. This is an entirely different problem relating to the details of your function. That's something for you to figure out. I'd suggest evaluating and plotting zr17t9001 as a function of lambda (in the range given by "bounds") to see what's going on, before trying to apply fzero (or, for that matter, any other root-finding algorithm).
zayed
zayed el 30 de Nov. de 2011
I put plot(zr17t9001 ,lambda) in the last line.But the same error
Walter Roberson
Walter Roberson el 30 de Nov. de 2011
Put a breakpoint in at the call to fzero (but before executing it.) Then at the command window, command
t = linspace(0, -1/gamma, 50);
z = zeros(size(t));
for K = 1 : length(t); z(K) = Lz(t(K)); end
plot(t,z);
zayed
zayed el 30 de Nov. de 2011
It gives "Not enough input arguments".
Matt Tearle
Matt Tearle el 1 de Dic. de 2011
What gives this error? Where is this error coming from? As a variation on Walter's suggestion, put these three lines *before* the call to fzero (and, ideally, comment out the fzero line).
l = linspace(0,-1/gamma);
z = arrayfun(Lz,l);
plot(l,z)
Do you get a plot? If so, does it look like something you were hoping for? In particular, does it have any zeros? You could also dive in and look at the values you're getting for z to see if your function is doing what you expect it to do.

Iniciar sesión para comentar.

Categorías

Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Nov. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by