Finding Roots between a Differential and Piecewise Equation

Hey all!
I am currently working on a project where I have to plot the roots between two of my functions with the help of fzero. I understand that I need to create a third function h(x) where h(x) = g(x) - f(x). But one of the functions is a piecewise equation. Therefore, I am not 100% sure how to find the roots between the two.
This is my piecewise function:
xp1 =@(t) ((-(8*2)/(2^2+1))*cos(t))+((8/(2^2+1))*sin(t));
x1 =@(t) (((8*2)/(2^2+1))+6)*exp(-t/2) + xp1(t);
x2 =@(t) x1(pi)*exp((pi-t)/a);
This is my other function:
fun1 =@(t) (((4+((8*2)/(2^2+1)))*exp(-2*t))+((8/(2^2+1))*(sin(t)-2*cos(t))))
I am able to plot both of them but I dont understand how to find the roots between these two. Any help would be appreciated.

1 Comment

This is my piecewise function:
In what way is it a piecewise function? You have listed three different functions, all presumably defined for all . Or have you left the intervals on which each is defined unspecified?

Sign in to comment.

 Accepted Answer

a=1;
[t_root,fval]=fzero(@(t)rootFcn(t,a), pi)
t_root = -0.1634
fval = 0
function out=rootFcn(t,a)
xp1 =@(t) ((-(8*2)/(2^2+1))*cos(t))+((8/(2^2+1))*sin(t));
x1 =@(t) (((8*2)/(2^2+1))+6)*exp(-t/2) + xp1(t);
x2 =@(t) x1(pi)*exp((pi-t)/a);
fun1 =@(t) (((4+((8*2)/(2^2+1)))*exp(-2*t))+((8/(2^2+1))*(sin(t)-2*cos(t))));
out=fun1(t);
if t<=pi
out=out-x1(t);
else
out=out-x2(t);
end
end

5 Comments

However fzero uses logic that depends upon continuous derivatives, and will not necessarily work on piecewise functions (even continuous ones)
Is that true? I don't see any indication of that in the doc.
The code is an m-function and doesn't appear to depend on continuous derivatives. Even this seems to come up with reasonable answer
f = @(x) sign(x);
fzero(f,1)
ans = -4.5963e-16
fzero(f,-1)
ans = 4.5963e-16
The algorithm, created by T. Dekker, uses a combination of bisection, secant, and inverse quadratic interpolation methods
I would note that all three of those methods involve interpolation mathematics that is not valid if the derivatives are not continuous.
Additional query: In this question, the solutions of the differential equations are considered. In case we were to use the numerical result obtained from an S-function code for the differential equations, would calling that within the rootFcn() work?

Sign in to comment.

More Answers (0)

Categories

Find more on Optimization in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!