Find values that put equation to zero

조회 수: 14 (최근 30일)
MARCO FORTI
MARCO FORTI 2021년 4월 9일
댓글: MARCO FORTI 2021년 4월 10일
In this model:
where all the variables are of dymension TX1, I need to find all the (T) values of gamma that put the equation to zero.
I tryed with the following code, but the program returns me all NAN, how can I fix the code to get it?
j=1; %increment level. The loop will work for j>1 as well.
for i=1:j:T;
fnc = @(gamma,C,S) 1./(1+gamma).*(C(i)-(S(i)*gamma)) - M(i); % Model function
rn = @(gamma) norm(M - fnc(gamma,C,S)); % Residual norm
gamma0 = [15]; % Inizializzazione
[gamma, ResNorm] = fzero(rn, gamma0);
end

채택된 답변

Matt J
Matt J 2021년 4월 10일
편집: Matt J 2021년 4월 10일
gamma=nan(T,1);
j=1; %increment level. The loop will work for j>1 as well.
for i=1:j:T;
ci=C(i); si=S(i); mi=M(i);
fnc = @(gamma) 1./(1+gamma).*(ci-(si*gamma)) - mi;
gamma0 = 15;
[gamma(i), ResNorm] = fzero(fcn, gamma0);
end
  댓글 수: 3
Matt J
Matt J 2021년 4월 10일
You may be initializing at a bad point. This works:
T=5;
[C,S,M]=deal(rand(T,1), rand(T,1), rand(T,1));
gamma=nan(T,1);
j=1; %increment level. The loop will work for j>1 as well.
for i=1:j:T;
ci=C(i); si=S(i); mi=M(i);
fnc = @(gamma) 1./(1+gamma).*(ci-(si*gamma)) - mi;
gamma0 = 15;
[gamma(i), ResNorm] = fzero(fnc, 0);
end
gamma
gamma = 5×1
-1.0000 0.0302 0.6949 0.1094 -0.4440
MARCO FORTI
MARCO FORTI 2021년 4월 10일
It works, thanks!

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

추가 답변 (1개)

Alan Stevens
Alan Stevens 2021년 4월 9일
편집: Alan Stevens 2021년 4월 9일
If your expression is equal to zero, then you can rearrange it to get gamma directly. A little algebra gives
gamma = (C-M)./(S+M);
No need for fzero at all.
  댓글 수: 1
MARCO FORTI
MARCO FORTI 2021년 4월 10일
Sorry, I mispelled the formula, now I corrected fixed it.
However I would like to understand how to develope a proper matlab code for this (or other similar) situations. Could you help?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by