Why the myfunction return zero elements?

조회 수: 1 (최근 30일)
Muna Shehan
Muna Shehan 2016년 7월 17일
댓글: Muna Shehan 2016년 7월 18일
Hi all; I have a function which should return a R_gradient matrix. I identify its elements but when I call myfunction it returns zero elements. I checked the passing parameter kc there is nothing wrong with it and the values of the elements are calculated correctly when i calculated them separately (i.e. Rg22,....,Rg55). can any one explain what wrong with myfunction. Regards
function R_gradient = myfunction(kc)
r1 = 1e+5;
r2 = 0.5;
q = 1e-5;
ks = kc(1);
cs = kc(2);
param.ms = 325;
param.mus = 65;
param.kus = 232.5e3;
param.ct = 0 ;
Rg22=r2*(cs/param.ms)^2;
Rg23=-r2*(cs/param.ms)*(ks/param.ms);
Rg24=-r2*(cs/param.ms)^2;
Rg25=r2*cs/(param.ms^2);
Rg32=-r2*(cs/param.ms)*(ks/param.ms);
Rg33=r2*(ks/param.ms)^2;
Rg34=r2*(cs/param.ms)*(ks/param.ms);
Rg35=-r2*ks/(param.ms^2);
Rg42=-r2*(cs/param.ms)^2;
Rg43=r2*(cs/param.ms)*(ks/param.ms);
Rg44=r2*(cs/param.ms)^2;
Rg45=-r2*cs/(param.ms^2);
Rg52=r2*cs/(param.ms^2);
Rg53=-r2*ks/(param.ms^2);
Rg54=-r2*cs/(param.ms^2);
Rg55=q+r2/(param.ms^2);
R_gradient = [
r1 0 0 0 0;...
0 Rg22 Rg23 Rg24 Rg25;...
0 Rg32 Rg33 Rg34 Rg35;...
0 Rg42 Rg43 Rg44 Rg45;...
0 Rg52 Rg53 Rg54 Rg55];
end

채택된 답변

Star Strider
Star Strider 2016년 7월 17일
I don’t know what your ‘kc’ is, but when I do this:
kc = [1 2];
R_gradient = myfunction(kc)
I get this:
R_gradient =
1e+05 0 0 0 0
0 1.8935e-05 -9.4675e-06 -1.8935e-05 9.4675e-06
0 -9.4675e-06 4.7337e-06 9.4675e-06 -4.7337e-06
0 -1.8935e-05 9.4675e-06 1.8935e-05 -9.4675e-06
0 9.4675e-06 -4.7337e-06 -9.4675e-06 1.4734e-05
I don’t understand the problem. What do you want it to do?
  댓글 수: 2
Muna Shehan
Muna Shehan 2016년 7월 18일
Thanks for your replay, the problem in kc values. kc values are passing parameters from another function where they store in Fkc(2:3) so what I did before is save these values in another vector and sent it to I sent kc elements as:
kc(1)=Fkc(2); % value come from another function
kc(2)=Fkc(3); % value come from another function
when I apply the above code before call myfunction(kc) I get this error. Now I do not save the required values in a vector just sent the required elements
|Fkc(2:3)| as:
R_gradient = myfunction(Fkc(2:3))
and its Ok R_gradient = myfunction(kc) did not return a matrix with zero elements. Thanks
Star Strider
Star Strider 2016년 7월 18일
My pleasure.
I do not understand what you want your ‘myfunction’ to do. If you are doing nonlinear optimisation and want to check its convergence, I would use the norm function with a tolerance. It will approach zero but will likely not ever uniformly equal zero.
If you are doing nonlinear optimisation, you need to use the output of ‘myfunction’ to change the values of ‘kc’ in your code from iteration to iteration until the gradient approximates zero within an acceptable tolerance. (The usual default tolerance is 1.0E-8 in most such applications.)
I am guessing what you are doing. This is the best I can do.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 7월 17일
If you getting out a matrix in which all except one entry are 0, then you need to give the command
format long g
and then look at the outputs again.
  댓글 수: 1
Muna Shehan
Muna Shehan 2016년 7월 18일
Thanks Walter for your replay. I give the command format long g to check the matrix but it seem the problem is with the passing parameter Fkc actually Fkc is not a passing parameter vector, its an optimization design vector which came from fmincon, but for some purpose I need to separate the elements of the design variable. By the way I learn some thing new "format long g" its new command for me Thanks alot

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

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by