Find and display intersections using fzero

조회 수: 16 (최근 30일)
Carlos
Carlos 2015년 3월 12일
댓글: Brendan Hamm 2015년 3월 13일
I have two functions: f(x) = x.^2-1, f(g) = exp(x/5) in a linspace(-2,2). I need to indicate on the graph where f = g using the fzero function. Also I would like to keep it in a single m file.
Thanks

채택된 답변

Brendan Hamm
Brendan Hamm 2015년 3월 12일
편집: Brendan Hamm 2015년 3월 12일
If f(x) = g(x) then f(x) - g(x) = 0. So we need to define h(x) = f(x) - g(x) and find where h(x) = 0. This is exactly what fzero is made for.
h = @(x) x.^2 - 1 - exp(x/5); % Creates a function handle for your function
Now let's plot and see if we can find a point close to where this function is zero.
x = -2:0.01:2;
plot(x,h(x));
Looks like -1.5 and 1.5 are close.
firstZero = fzero(h,-1.5);
secondZero = fzero(h,1.5);
Now visualize these on the plot:
hold on
plot(firstZero,h(firstZero),'ro');
plot(secondZero,h(secondZero),'ro');
hold off
  댓글 수: 2
Carlos
Carlos 2015년 3월 12일
Thanks that's exactly what I needed. I used f(firstZero) and g(firstZero) rather than h to graph the point of intersection.
Brendan Hamm
Brendan Hamm 2015년 3월 13일
Awesome. If you found this to solve your problem, please accept the answer so others with similar issues can find the solution.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by