CODE

조회 수: 1 (최근 30일)
zayed
zayed 2011년 11월 29일
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

채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 29일
[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
zayed
zayed 2011년 11월 30일
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 2011년 11월 30일
Sorry, I have other things to do. You can find plenty of Newton Raphson implementations around, as the technique is a common course assignment.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Tearle
Matt Tearle 2011년 11월 29일
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
zayed
zayed 2011년 11월 30일
It gives "Not enough input arguments".
Matt Tearle
Matt Tearle 2011년 12월 1일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by