How do I plot the intersections of two functions???

조회 수: 5 (최근 30일)
Mark Dillon
Mark Dillon 2015년 7월 30일
댓글: Star Strider 2015년 7월 30일
I have been tasked with plotting two functions and having to find 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.
This is what I have worked out so far, but I cannot figure out how to show the 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]);
end
[EDIT] 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.

채택된 답변

Star Strider
Star Strider 2015년 7월 30일
One possibility:
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
  댓글 수: 1
Star Strider
Star Strider 2015년 7월 30일
Improved version:
F1 = @(x) 90.*exp(-x) - 1;
F2 = @(x) sin(2.*pi.*x);
x = linspace(3, 8, 150); % Domain ‘x’
fcndif = @(x) F1(x) - F2(x); % Function Differences
zx = fcndif(x) .* circshift(fcndif(x), [0 -2]); % 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
intsct = unique(round(intsct*10^6)./10^6);
figure(1)
plot(x, F1(x), x, F2(x))
hold on
plot(intsct, F1(intsct), 'bp')
hold off
grid

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jon
Jon 2015년 7월 30일
I don't know if there's a built-in code for finding intersections from function objects, but you could use the intersections.m function from the FEX.
rng = 3:.05:8;
f1 = F1(rng);
f2 = F2(rng);
[xints,yints,~,~] = intersections(rng,f1,rng,f2,1);
hold on
plot(xints,yints,'r*')
will result in the following image:
  댓글 수: 2
Mark Dillon
Mark Dillon 2015년 7월 30일
I could do it that way, but I need to do it using zero somehow in an for or while loop.
Jon
Jon 2015년 7월 30일
편집: Jon 2015년 7월 30일
Oh, I didn't realize I was doing your homework. You want to find when F1 = F2 (i.e., their intersection). Just rearrange the equation and you see that F1-F2=0 (at their intersection). Now you have a new function (F1-F2) whose roots are the intersections of F1 with F2. The hints basically spell it out.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by