Clear Filters
Clear Filters

Why the myfunction return zero elements?

2 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 17 Jul 2016
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 Comments
Muna Shehan
Muna Shehan on 18 Jul 2016
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 on 18 Jul 2016
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.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Jul 2016
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 Comment
Muna Shehan
Muna Shehan on 18 Jul 2016
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!