solve a function on intervalls
Show older comments
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
Walter Roberson
on 26 Jun 2020
(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
on 26 Jun 2020
Edited: Marius Brettner
on 26 Jun 2020
Walter Roberson
on 26 Jun 2020
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?
Marius Brettner
on 26 Jun 2020
Walter Roberson
on 26 Jun 2020
Edited: Walter Roberson
on 26 Jun 2020
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)
Marius Brettner
on 26 Jun 2020
Walter Roberson
on 26 Jun 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)
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!