# Finding Roots between a Differential and Piecewise Equation

10 views (last 30 days)
Mike on 25 Jan 2023
Commented: Walter Roberson on 27 Jan 2023
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.
Matt J on 25 Jan 2023
Edited: Matt J on 25 Jan 2023
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?

Matt J on 26 Jan 2023
Edited: Matt J on 26 Jan 2023
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
Walter Roberson on 27 Jan 2023
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.

### Categories

Find more on 2-D and 3-D Plots 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!