Newton's Method Graphing
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nicholas Fuller
le 31 Mar 2021
Commenté : Nicholas Fuller
le 2 Avr 2021
Hey everyone, I need some assitance in plotting my code. I have the computational work done, I just need help plotting the values. My goal is to plot the original function, then the approximated points along the original curve in a different color to be able to see the accuracy. I was thinking of creating the plot in the while loop, but ran into issues where multiple graphs would appear, or the variable I wanted to be the y axis would be zero. I was also having problems trying to store the value in the while loop without being constantly overwritten.
Thank you for your time and help!
Below is the code, feel free to edit or use it for whatever you may need.
clc; clear all; close all;
format long
functn = @(x) 2*sin(x) - x %Starting function
interval = 0.001; %Small x-step used to approximate the function derivative
x = 0.01:0.01:1; %Range of x
dydx = (functn(x+interval) - functn(x))/interval; %Definition of a derivative
Root_value = 0.069; % Function value
ytolerance = 1e-6; % The convergence tolerance
initial_guess = 2; % The initial guess for the location of the root x0
x = initial_guess; %Setting x to be the initial guess, using initial guess to compute the function
yerror = Root_value-functn(x); %Computing error
counter = 1; %A basic loop counter
previous_x = 0; %Setting previous x value to be empty, "0"
t = 0; %Iteration counter
fprintf('Iteration#\t\t x\t\tf(x)\t\terror\n') %Output headers
while abs(yerror)>ytolerance %While the absolute value of the error > error tolerance, the loop continues
dydx = (functn(x+interval)-functn(x))/interval; %Definition of a derivative
fx=functn(x); %Renaming function to something simpler
dx = fx/dydx; %Original function divided by the derivative
previous_x=x; %Setting the previous x value to the current x value to progress the loop
x=x-dx; %Definition of Newton's Method
yerror = Root_value - functn(x); %Recalculating error
counter = counter+1; %A basic loop counter
if previous_x==x %Once the previous x value equals the current x value, the loop breaks, and the program ends
break
end
t = t+1; %Iteration counter
fprintf('%d\t\t\t%f\t%f\t%f\n',t,x,fx,yerror) %Outputting all values at each iteration
end
2 commentaires
Réponse acceptée
darova
le 2 Avr 2021
Try this way
%Plotting
xx = 0:0.1:1;
figure
plot(xx,functn(xx),'k') %Plotting original funtion
hold on
plot(initial_guess,functn(initial_guess), 'k', "Marker","o") %Plotting the initial guess value
while % condition
% some code
plot(x,functn(x), 'r', 'Marker', "*") %Plotting approximated root value
end
hold off
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!