solve a function on intervalls

13 visualizzazioni (ultimi 30 giorni)
Marius Brettner
Marius Brettner il 26 Giu 2020
Commentato: Walter Roberson il 26 Giu 2020
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 Commenti
Marius Brettner
Marius Brettner il 26 Giu 2020
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 il 26 Giu 2020
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)

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by