How do I plot multiple intersections between two functions?

9 views (last 30 days)
I have been tasked with plotting two functions as well as marking where the two intersect. I also have to use a for or while loop to automatically find all of the intersections in the given domain and print the results of those intersections.
if true
% code
%%Find all of the intersections of two functions in the domain of x
% Variable 3 ≤ x ≤ 8
% Functions
F1 = @(x) 90.*exp(-x) - 1;
F2 = @(x) sin(2.*pi.*x);
fplot(F1,[3,8]);
grid on;
hold on;
fplot(F2,[3,8]);
for k = 1:7
int = fzero(@(x) F1(x)-F2(x),k); % The x-coord
plot(int,F1(int),'*k'); % The coordinates.
fprintf('Intersection %0.0f is at x = %0.4f\n', k, int);
end
end
My for loop is not working the way I need it to, so any help would be excellent. Here are a couple hints I have been given:
Hint 1: How do you make an anonymous function that takes one variable (x) and returns zero when the two functions intersect?
Hint 2: You need to call fzero a bunch of times with a reasonable set of guesses, enough to make sure that you actually get all of the intersections. Each time you calculate a new intersection, compare it to ALL of the intersections that you have already calculated. If the difference between the new intersection and any of the old ones is very small (<0.00006), do not add it to your list and just move on to calculating the next one. Don’t forget to check that the intersection is in the given domain.

Accepted Answer

Star Strider
Star Strider on 30 Jul 2015
This works:
F1 = @(x) 90.*exp(-x) - 1;
F2 = @(x) sin(2.*pi.*x);
x = linspace(3, 8, 500); % Domain ‘x’
fcndif = @(x) F1(x) - F2(x); % Function Differences
zx = fcndif(x) .* circshift(fcndif(x), [0 -1]); % Detect Zero-Crossings
gues = find(zx <= 0); % Find Indices Of Zero Crossings
for k1 = 1:length(gues)
intsct(k1) = fzero(fcndif, x(gues(k1))); % Find ‘x’ at Zero Crossings
end
figure(1)
plot(x, F1(x), x, F2(x))
hold on
plot(intsct, F1(intsct), 'bp')
hold off
grid
  7 Comments
Mark Dillon
Mark Dillon on 31 Jul 2015
Thank you,
I am still having trouble printing the x values for intersection. Instead of 9 I get 20, a few of them are repeats. All I did was
if true
%
for k = 1:length(f)
int(k) = fzero(D, x(f(k))); % Find ‘x’ at intersections
% Printing
fprintf('Intersection Number %0.0f, x = %0.0f\n',k, x(k));
end
end
I have tried playing around with it, and in it's current form it isn't even outputting the right numbers now. I messed up somewhere...
Star Strider
Star Strider on 31 Jul 2015
My pleasure.
As for printing, using my code, and putting this just before (or just after) the figure(1) block:
intsnr = [1:length(intsct); intsct; F1(intsct)];
fprintf(1, 'Intersection Number %.0f: x = %.4f, y = %+.4f\n', intsnr)
Intersection Number 1: x = 4.0857, y = +0.5130
Intersection Number 2: x = 4.5000, y = -0.0002
Intersection Number 3: x = 4.9419, y = -0.3573
Intersection Number 4: x = 5.6175, y = -0.6730
Intersection Number 5: x = 5.8662, y = -0.7450
Intersection Number 6: x = 6.6733, y = -0.8862
Intersection Number 7: x = 6.8211, y = -0.9019
Intersection Number 8: x = 7.7045, y = -0.9594
Intersection Number 9: x = 7.7935, y = -0.9629
If you don’t want the ‘+’ sign but you still want the columns to be aligned, use this:
fprintf(1, 'Intersection Number %.0f: x = %.4f, y = % .4f\n', intsnr)
Note that the space in: % .4f is significant, and leaves a space instead of printing the sign if the value for that descriptor is positive.
Also, you don’t need a loop to print it. Take advantage of MATLAB’s matrix properties with fprintf. (I just realised that I specified 1 or stdout in fprintf out of habit. That used to be required.)

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!