CODE ASSISTANCE

Hi, I have the following code ,but it gives an error. The code is :
function lambda = drive_zr17t9(p)
format short e;
load saved_data;
theta = pi/2;
zeta = cos(theta);
I = eye(n,n);
Q = zeta*I-p*p';
%T is a matrix(5,5)
Mroot = M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E = eig(T);
%find the negative eigen values -- this section still needs to be fixed
ind = find(E<0);
G = E(ind);
%find the smallest negative eigen value
gamma = min(abs(G)); %this is almost certainly wrong!
%now solve for lambda
bounds = sort([0, -1/gamma]); %in case gamma is positive
Minv = inv(M); %optimization
lambda = fzero(@(lambda) zr17t9(lambda, Minv, Q, zm), bounds); %do the searching
end
function r = zr17t9(lambda, Minv, Q, zm)
Winv = inv(mInv+lambda.*Q);
r = -ctranspose(zm)*Minv*Winv*Q*Winv*Minv*zm;
end
_*The error is :
??? Error using ==> fzero
FZERO cannot continue because user supplied function_handle ==> @(lambda) zr17t9(lambda, Minv, Q, zm)
failed with the error below.
Variable 'zm' is used as a command function.
Error in ==> drive_zr17t901 at 21
lambda = fzero(@(lambda) zr17t9(lambda, Minv, Q, zm), bounds); %do the searching
_*

 採用された回答

Walter Roberson
Walter Roberson 2011 年 11 月 28 日

0 投票

Remove the final "end" statement from drive_zr17t9 and the code would likely start working.
This is a side effect of "poofing variables into existence" through a load() statement.
Matt's approach is a better one over the long term.
[Addition: code with Matt's suggested changes]
[Code modified to reflect the fact p is in the .mat file after all]
[Code modified: Minv -> M_inv]
function lambda = drive_zr17t9
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 = eig(T);
%find the negative eigen values -- this section still needs to be fixed
ind = find(E<0);
G = E(ind);
%find the smallest negative eigen value
gamma = min(abs(G)); %this is almost certainly wrong!
%now solve for lambda
bounds = sort([0, -1/gamma]); %in case gamma is positive
M_inv = inv(Params.M); %optimization
lambda = fzero(@(lambda) zr17t9(lambda, M_inv, Q, Params.zm), bounds); %do the searching
end
function r = zr17t9(lambda, M_inv, Q, zm)
Winv = inv(M_inv+lambda.*Q);
r = -zm'*M_inv*Winv*Q*Winv*M_inv*zm;
end

17 件のコメント

zayed
zayed 2011 年 11 月 28 日
It gives me a new error:
Error: File: C:\MATLAB7\work\project\drive_zr17t9.m Line: 26 Column: 39
The function "drive_zr17t9" was closed
with an 'end', but at least one other function definition was not.
To avoid confusion when using nested functions,
it is illegal to use both conventions in the same file.
Walter Roberson
Walter Roberson 2011 年 11 月 28 日
Remove the trailing "end" in the second function too.
Walter Roberson
Walter Roberson 2011 年 11 月 28 日
Look back at the "lambda =" line in your posting. Notice the positions of the ")". Compare to the positions you have now.
zayed
zayed 2011 年 11 月 28 日
error at line 21 :
Variable 'zm' is used as a command function.
Walter Roberson
Walter Roberson 2011 年 11 月 28 日
Hmmm.... in that case, go with Matt's solution.
zayed
zayed 2011 年 11 月 29 日
I went to Matt's ,and i defined n,p as params.n&params.p,one by one and both but there is an error like
Undefined function or variable 'n'....for n
and when i defined n=5;it gives
Input argument "p" is undefined.
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
See addition to my Answer for the code amended according to Matt's suggestion.
If it says Params.n is not a known field of Params then please show the output of
whos -file saved_data.mat
zayed
zayed 2011 年 11 月 29 日
I saw the new code with suggestion ,but it gives error:
Input argument "p" is undefined.
Also i showed the output of saved_data,all the inputs are involved
Name Size Bytes Class
Decision 1x1000 8000 double array
E 5x1 40 double array
FD 1x1 8 double array
I 5x5 200 double array
K 1x1 8 double array
L 1x1 8 double array
M 5x5 200 double array
Mz 5x5 200 double array
PRF 1x1 8 double array
Q 5x5 400 double array (complex)
T 5x5 400 double array (complex)
U 1x1 16 double array (complex)
W 5x5 400 double array (complex)
ans 5x1 80 double array (complex)
d 1x1 8 double array
dsss 1000x1 8000 double array
eta 1x1 8 double array
fD 1x1 8 double array
gamma 1x1 8 double array
h1 1x1 8 double array
h2 1x1 16 double array (complex)
i 1x1 8 double array
k 1x1 8 double array
lambda 1x1 8 double array
m 1x1 8 double array
n 1x1 8 double array
p 5x1 80 double array (complex)
pfa 1x1 8 double array
radar_noise 5000x1 40000 double array
radar_pulse 99x1 792 double array
radar_received 5000x1 40000 double array
s 5000x1 40000 double array
t 1x1 8 double array
theta 1x1 8 double array
x 5000x1 40000 double array
x1 1x1 8 double array
x2 1x1 8 double array
zeta 1x1 8 double array
zk 5x1000 40000 double array
zm 5x1 40 double array
Grand total is 27291 elements using 219024 bytes
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
And remember, this code does not attempt to fix the determination of gamma, as that part needed some feedback from you. In particular, you need to clarify what it means to you to compare complex numbers to find the minimum of them.
http://www.mathworks.com/matlabcentral/answers/22336-code-assistance
and be sure to click on the "Show 6 older comments"
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
Code has been modified in the above answer to reflect that p is stored in the .mat file after all.
zayed
zayed 2011 年 11 月 29 日
I have fixed determination of gamma,by choosing the the minimum of the real part of eigen value.
There was an error in typing:Winv = inv(mInv+lambda.*Q);i have fix it.
But it gives error
??? Error using ==> fzero
Function values at interval endpoints must be finite and real.
Error in ==> drive_zr17t901 at 21
lambda = fzero(@(lambda) zr17t901(lambda, Minv, Q, Params.zm), bounds); %do the searching
By the way why you give the function such name?
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
I speculate that either gamma was created as complex, or else that the zr17t9 calculation comes out as complex. You should put in a breakpoint in zr17t9 and trace the execution and see what value is input and whether r is real or complex.
As for why I gave the function such a name... Why not? ;-) There was no obvious name for what the function was doing (I didn't even know you were doing radar work until you showed the "whos" output), so I just invented an name that was highly unlikely to clash with anything else you had.
zayed
zayed 2011 年 11 月 29 日
gamma=-2.8797e-002.when i executed Minv it gives :
Minv
usage: out = minv(mat)
When i execute zr17t901(lambda , Minv, Q, zm),it gives:
usage: out = minv(mat)
??? One or more output arguments not assigned during call to 'C:\MATLAB7\toolbox\mutools\commands\minv.m (minv)'.
Also what is the difference between zr17t9 & derive_zr17t9
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
I adjusted the code above to rename Minv to M_inv so that you do not conflict with the minv routine.
drive_zr17t9 is what is often known in programm as a "driver" routine, which is a routine that exists to set up the conditions to be able to call the routine that does the real work. The real work is done by fzero(), which invokes zr17t9 to do the calculations.
The zr17t9 routine assumes that the variables have already been read in and have already been checked for reasonableness, whereas the drive_zr17t9 routine reads the values in and (should) check them for reasonableness.
Matt Tearle
Matt Tearle 2011 年 11 月 29 日
Looks like Walter missed an Minv. It should be M_inv.
zayed
zayed 2011 年 11 月 29 日
I executed M_inv it gives:
Undefined variable "Params" or class "Params.M".
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
You cannot "execute" M_inv . M_inv is a variable that is defined within the code, based upon previous variables defined in the code.
You can put a breakpoint in at the M_inv line and run to there, and step and examine the value of M_inv . You cannot execute the line out of context.

サインインしてコメントする。

その他の回答 (1 件)

Matt Tearle
Matt Tearle 2011 年 11 月 28 日

1 投票

I'm guessing it's a name conflict issue caused by your load command. Is zm loaded by the command load saved_data;? If so, do
params = load('saved_data');
...
... zr17t9(lambda, Minv, Q, params.zm) ...
instead, and similarly for any other variables loaded from saved_data. (If not, how the heck is zm defined?)
Also: %this is almost certainly wrong! is a fantastic comment :)
EDIT TO ADD An attempt at explaining what's happening here. (Note: technical details are probably not exactly correct, but close enough.) As you know, MATLAB is an interpreted language, but when code is executed, it isn't all interpreted on the fly -- there's some "pre-interpretation" where MATLAB parses, checks, optimizes, caches, and good stuff like that. When it sees x = foo, it has to resolve what x and foo mean. They could be variables, they could be function calls. The assignment x = tells MATLAB that x is going to be a variable. But if there's no foo = earlier in the code, then foo had better be a function call, otherwise you're going to get the infamous "undefined function or variable" error.
OK, so now look at your code. Nowhere is zm defined, as far as MATLAB can tell. Functions maintain their own private workspaces, so zm can't be magically inherited -- if it isn't explicitly passed in as an input, it doesn't exist. Hence, this, as far as MATLAB is concerned, is a function call. But then, you zap a variable called zm into existence with the load command. This causes a major headache for MATLAB. What does zm mean?
The solution is: don't zap variables into existence with load inside functions. If you want to load variables from a MAT-file, use the functional form: x = load('filename'). This creates a structure variable x, with fieldnames defined by the variables in the MAT-file. Hence, if filename.mat contains x, y, and foo, then you will now have a structure x with fields x, y, and foo, which you can reference with structure/dot notation: x.x, x.y, and x.foo.
(Note that the assignment x = load(...) means that MATLAB knows that x will be a variable, so the name conflict issue goes away.)
Bottom line: In your code, you appear to be relying on variables n, M, and zm being created by the load. So replace those with params.n, params.M, and params.zm, respectively.

5 件のコメント

zayed
zayed 2011 年 11 月 28 日
I did your suggestion,but it gives an error in line 6,and the error is
'' undefined function or variable 'n' ''.
But it defined in the saved_data (n=5).
Walter Roberson
Walter Roberson 2011 年 11 月 28 日
Matt wrote, "and similarly for any other variables loaded from saved_data"
Matt Tearle
Matt Tearle 2011 年 11 月 28 日
Right, so n becomes params.n, p (by the looks of it) becomes params.p, etc. Anything loaded from the file will now be params.[variable name].
zayed
zayed 2011 年 11 月 28 日
''Incomplete or misformed expression or statement.'' FOR the expression below:
lambda = fzero(@(lambda) zr17t9(lambda, Minv, Q, zm, bounds)
Walter Roberson
Walter Roberson 2011 年 11 月 28 日
You somehow deleted the ")" that goes after the "zm".

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by