How do I plot multiple intersections between two functions?
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Star Strider
2015-7-30
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 个评论
Star Strider
2015-7-31
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.)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!