solve a function on intervalls

13 ビュー (過去 30 日間)
Marius Brettner
Marius Brettner 2020 年 6 月 26 日
コメント済み: Walter Roberson 2020 年 6 月 26 日
Hello everyone,
today I have I problem which I could not solve because of the limitation of my matlab skills. I am sure Matlab can do this . So time to learn something new - I hope you can teach me how to :D
I will try to explain what I want to do first :)
My function is F(s,x_i). The function is defined on three intervals for s which are known (s<s_a, s_a<=s<=s_b, s_b<s). X is a set of known parameters. We have two known x called x_1 and x_2 which are different. The values of the intervals s_a and s_b are known but different for both x parameter sets. S is what we are looking for. To solve this I have the condition of the sum of F(s,x_1) and F(s,x_2) to match a known value F_total.
So F_total = F(s,x_1) + F(s,x_2) is what we want so solve to get our s.
The simple and stupid idea was to increase s from minimum value till we reach F_total. But this was much too slow, so I need a more efficient way to get the solution :(
I hope you can help me, thank you very much in advance!
Marius
  7 件のコメント
Marius Brettner
Marius Brettner 2020 年 6 月 26 日
Thank you very much! fzero seems to be what I was looking for!
One thing I don´t get: just how do I manage the diffrent intervals of my function. Since it´s a broken rational function on the different intervals the function is definded by a diffrent expression. And when defining the function (handle) which I put in fzero I need already to know in which interval s will be, but I don´t know at that moment.
For example for
s<s_a: F(s)=F1(s)
s_a<=s<=s_b: F(s)=F2(s)
s_b < s: F(s)=F3(s)
How do I get this in the function of fzero whithout knowing s?
Thank you very much again!!
Walter Roberson
Walter Roberson 2020 年 6 月 26 日
F = @(s) (s<s_a) .* F1(s) + (s_a <= s & s < s_b) .* F2(s) + (s_b < s) .* F3(s);
However, this will fail if any of the functions could return infinity or nan when invoked "when they shouldn't be". For example if F2 included 1/s and that wasn't supposed to be a problem because s_a and s_b where chosen such that the range for F2 excluded s = 0, then there would be a problem.
For cases that can include infinity or nan, you need more complicated phrasings such as
H = {@F1, @F2, @F3};
F = @(s) H{cumsum([1, s_a <=s, s_b <s])}(s)

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by