How do I make a graph for newton raphson?

12 views (last 30 days)
% Newton Raphson Method
clc
clear all
close all
N = 30; % NO Iterations
err = 0.005; % Result Accuracy
% Equation to Solve
f = @(x) (9 * exp(-0.7*x) * cos (4*x)) - 3.5;
fdiff = @(x) - (63*cos(4*x)*exp(-(7*x)/10))/10 - 36*sin(4*x)*exp(-(7*x)/10);
%t = 0.1:0.1:1;
%y = f(t);
%figure
%plot(t,y);
x = 0; % Initial Value
xx(1) = x; % X History
fprintf('\n Itr. No.\t \t Xi\t \tf(Xi)\t\tfdiff(Xi)\t Ea\t \n')
Itr. No. Xi f(Xi) fdiff(Xi) Ea
for i = 2:N
x = x - f(x) / fdiff (x);
xx(i) = x; ii = i-1;
Err = abs(xx(i) - xx(i-1)); if Err < err, break; end
fprintf('\n \t%.0f\t \t%.4f\t \t%.4f\t \t%.4f\t \t%.2f\t',i,x,f(x),fdiff(x),Err)
end
2 0.8730 -8.0878 9.9199 0.87 3 1.6883 -1.0390 -6.7244 0.82 4 1.5338 -0.4578 -0.3161 0.15 5 0.0855 4.4858 -16.9649 1.45 6 0.3499 -2.3010 -28.6067 0.26 7 0.2695 0.0257 -28.7318 0.08
fprintf('\n\n\t')
disp(['The Root is: ' num2str(x) ' , with accuracy: ' num2str(Err) ' , No. Iterations: ' num2str(ii)])
The Root is: 0.2704 , with accuracy: 0.00089289 , No. Iterations: 7
% Plotting Graph
x = linspace (-5,5);
Y = f(x);
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

Error in solution>@(x)(9*exp(-0.7*x)*cos(4*x))-3.5 (line 11)
f = @(x) (9 * exp(-0.7*x) * cos (4*x)) - 3.5;
subplot(2,1,1)
plot (x,Y,'linewidth',2)
xlabel ('Time, (s)'); ylabel ('Distance,(y)')
ax=gca;
ax.XAxisLocation = 'origin'
ax.YaxisLocation = 'origin'
box 'off'

Accepted Answer

the cyclist
the cyclist on 22 Jun 2023
I edited your anonymous functions to use element-wise multiplication (.*) instead of matrix multiplication (*).
% Newton Raphson Method
clc
clear all
close all
N = 30; % NO Iterations
err = 0.005; % Result Accuracy
% Equation to Solve
f = @(x) (9 * exp(-0.7*x) .* cos (4*x)) - 3.5;
fdiff = @(x) - (63*cos(4*x).*exp(-(7*x)/10))/10 - 36*sin(4*x).*exp(-(7*x)/10);
%t = 0.1:0.1:1;
%y = f(t);
%figure
%plot(t,y);
x = 0; % Initial Value
xx(1) = x; % X History
fprintf('\n Itr. No.\t \t Xi\t \tf(Xi)\t\tfdiff(Xi)\t Ea\t \n')
Itr. No. Xi f(Xi) fdiff(Xi) Ea
for i = 2:N
x = x - f(x) / fdiff (x);
xx(i) = x; ii = i-1;
Err = abs(xx(i) - xx(i-1)); if Err < err, break; end
fprintf('\n \t%.0f\t \t%.4f\t \t%.4f\t \t%.4f\t \t%.2f\t',i,x,f(x),fdiff(x),Err)
end
2 0.8730 -8.0878 9.9199 0.87 3 1.6883 -1.0390 -6.7244 0.82 4 1.5338 -0.4578 -0.3161 0.15 5 0.0855 4.4858 -16.9649 1.45 6 0.3499 -2.3010 -28.6067 0.26 7 0.2695 0.0257 -28.7318 0.08
fprintf('\n\n\t')
disp(['The Root is: ' num2str(x) ' , with accuracy: ' num2str(Err) ' , No. Iterations: ' num2str(ii)])
The Root is: 0.2704 , with accuracy: 0.00089289 , No. Iterations: 7
% Plotting Graph
x = linspace (-5,5);
Y = f(x);
subplot(2,1,1)
plot (x,Y,'linewidth',2)
xlabel ('Time, (s)'); ylabel ('Distance,(y)')
ax=gca;
ax.XAxisLocation = 'origin'
ax =
Axes with properties: XLim: [-5 5] YLim: [-200 300] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.5838 0.7750 0.3412] Units: 'normalized' Show all properties
ax.YaxisLocation = 'origin'
Unrecognized property 'YaxisLocation' for class 'matlab.graphics.axis.Axes'.
box 'off'

More Answers (0)

Community Treasure Hunt

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

Start Hunting!