Newton's Method Graphing

14 次查看(过去 30 天)
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 个评论
darova
darova 2021-3-31
Can you make a drawing or something?
Nicholas Fuller
Nicholas Fuller 2021-4-2
编辑:Nicholas Fuller 2021-4-2
So this plot is very close to what I am trying to do. Basically, at each iteration, a new point would appear on the graph with the other points still on the graph. I made a slight edit to my code where I am able to get plots and points; however, the plots are overwritten at each iteration of the loop.
For the example where f(x) = 2*sin(x) - x, I know that the plotted root approximation points, the red *, will be very close to each other and almost indistinguishable.
Here is the new code:
clc;
clear all;
close all;
format long
functn = @(x) 2*sin(x) -x %Starting function %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
%Plotting
figure
fplot(functn) %Plotting original funtion
hold on
plot(x, 'r', 'Marker', "*") %Plotting approximated root value
plot(initial_guess, 'k', "Marker","o") %Plotting the initial guess value
hline = refline(0, 0); %Refernce line to act as x-axis
hline.Color = 'k';
end
hold off

请先登录,再进行评论。

采纳的回答

darova
darova 2021-4-2
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by