solve a function on intervalls

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 Comments

(s<s_a, s_a<=s<=s_b, s_b<s)
For any given x_i, those three values are constants in x_i, but you do not know where the s_a, s_b breakpoints are, and your goal is effectively to find those s_a and s_b values? I know you say that the goal is to find s, but if the values are independent of s (but dependent on x_i) within each of the intervals, then determining the s is pretty much the same as finding the two breakpoints.
Or are the s_a and s_b breakpoints known, and within each interval the value is dependent upon s as well as x_i ? If so then how do you know that there are not multiple s values that lead to the same output?
Marius Brettner
Marius Brettner on 26 Jun 2020
Edited: Marius Brettner on 26 Jun 2020
First, thank you for your answer! I will try to make it more clear.
The interval boundaries s_a and s_b are known because I can calculate them out of the x_i parameters. So s_a and s_b depend on x_i which is why in the F_total = F(s,x_1) + F(s,x_2) problem F(s,x_1) can be on a diffrent interval then F(s,x_2).
For all parameters the graph of F(s) is rising to an maximum (first interval) and then falling to an constant (lower then maximum) value (interval two). The third interval for s>s_b is just a constant F. The F_total is limited, so I know that I can find a solution s. In some cases there might be two solutions for s but thats no problem because I only care for the smaller s.
Maybe I did not explain it very well. I think a simple "example" would help:
Known are x_1, x_2 and the intervals s_a_1, s_b_1, s_a_2, s_b_2 which seperate the function F. The target for F_total is known as well. Now I started with a low s and calculated F(s,x_1) and F(s,x_2). Because I did this "stupid" iteration I knew on which interval I was and how F(s,x) was defined due to this. Last I checked if the sum of F(s,x_1) and F(s,x_2) matched the F_total target. If not I repeated for a bigger s until F_total was reached.
It looks to me as if want you are trying to say is that the integral of F(s,x_i) is known and the question is to determine s given x_i such that the integral from some fixed value to s produces the desired value?
Yes, I think you got what I want!
Most easy solution would be something like write F_total = F(s,x_1) + F(s,x_2) to something like s(F_total,x_1,x_2). But I think this is not possible because before knowing s I can´t know the interval and so the current definition for F(s,x_i). So we need something like a iteration?
Use fzero() or fsolve(), integral() of some function from known lower bound to (parameter) s, and subtract from that the known value of the integral.
for example,
known = 2;
fzero(@(X) integral(@(x) tan(x).^2 + exp(x), -1, X) - known, 0)
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!!
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)

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 26 Jun 2020

Commented:

on 26 Jun 2020

Community Treasure Hunt

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

Start Hunting!