MATLAB Answers

0

Finding intersection using Newtons method

Asked by Praviin Tangavellu on 25 Aug 2019
Latest activity Answered by darova
on 25 Aug 2019
Hello there, I am trying to find the intersection of two functions, however there is two points of intersection
I have got a script to find an intersection but not sure how to find the second one as well.
this is the script I currently have
clear
clc
close all
% g = @(x) (1/3)-(3/4)*x ;
% dg = -3/4 ;
% h = @(x) 2*sin(x.^2);
% dh = @(x) 4*x*cos(x.^2) ;
N = 20 ;
tol = 0.001 ;
f = @(x) (1/3)-(3/4)*x -2*sin(x.^2);
df =@(x) -4*x*cos(x.^2)-3/4;
ff =@(x) g(x) - h(x);
x = -1.8;
for ii = 1 : 1 : N ;
Newx = x - (f(x)/df(x));
rootsArray(ii)= x ;
answ = (abs(Newx-x));
if ((abs(x-Newx)) < tol) && ((abs(f(Newx)))<tol);
break
end
x = Newx;
end
rootsArray(ii+1) = x;
rootsArray(end)

  0 Comments

Sign in to comment.

2 Answers

Answer by John D'Errico
on 25 Aug 2019

Firt, why are you writing rootfinding code, when tools like fzero exist? Don't write poorly written code when well written code already exists, and you already have that code!
Next, don't write things like a solver inline. Learn to use functions. Learn how to write your own function m-files, that contain code, something you will use more than once.
Next, whatever solver you use, you shpuld understand the solution you find depends on the starting value. Here you found a solution when you started at x=-1.8. Had you started the solver at a different, intelligently chosen point, you would have found the other solution. So the answer is rather simple, just call the solver TWICE, with different start points.
But don't use Newton's method. Don't write your own code for something that already exists. That means you need to start learning what tools are available already in MATLAB. You paid for the package, so why not learn to use the full capability of that tool? fzero is is not even part of a toolbox, but in MATLAB itself.

  0 Comments

Sign in to comment.


Answer by darova
on 25 Aug 2019

The first problem is when you found a root x stops to change
Newx = x - (f(x)/df(x)); % always the same because f(x) == 0
The second problem:
img1.png
When you are at P (green point) your df() is large.
Maybe Newtons meton is not the best for this problem

  0 Comments

Sign in to comment.