Find values that put equation to zero
3 ビュー (過去 30 日間)
古いコメントを表示
In this model:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/579282/image.png)
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
0 件のコメント
採用された回答
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
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
その他の回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Gamma Functions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!