clear all
close all
clc
format long
%Initiate limits a and b as well as the tolerance
a = -2;
b = 2;
tolerance = 1e-8;
%Initiate x to be 100 points going from a to b
x=linspace(a,b);
%Plot the function
% plot(x,f(x));
%Initiate n at 1
n = 1;
%Initiate the first point r(1) somewhere between a and b
r(n) = 1.9;
%Initiate r(2) using Newton's method
r(n+1) = r(n)-f(r(n))/fprime(r(n));
%Exact value given the tolerance above
exact_value = 1.530133508166616;
i = 0;
hold on
while abs(r(n+1)-r(n)) > tolerance %Loop will run as long as the difference between r(n) and r(n+1)
% is bigger than the tolerance
i=i+1;
r(n)=r(n+1); %Update r(n)
r(n+1) = r(n)-f(r(n))/fprime(r(n)); %Update r(n+1)
e_i = abs(r(n)-exact_value);
plot(i,e_i)
end
hold off

3 comentarios

Star Strider
Star Strider el 13 de Feb. de 2018
In the future, please highlight your code and use the [{}Code] button to format it.
Beshr Alalawi
Beshr Alalawi el 13 de Feb. de 2018
Sorry it's my first post. Thanks for the advice
Star Strider
Star Strider el 13 de Feb. de 2018
No worries!
I mentioned it because it’s your first post. My pleasure.

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 13 de Feb. de 2018
The default behaviour of plot is to connect two points with a line. If it only gets one point, it plots nothing. If you give it a marker, it will plot the marker.
Try this:
plot(i, e_i, 'pg')

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Feb. de 2018

Comentada:

el 13 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by