MATLAB Answers

MATLAB Newton Raphson Method with a function with array/matrix variables

53 views (last 30 days)
Kenneth Chia
Kenneth Chia on 23 Feb 2021
Edited: Kenneth Chia on 24 Feb 2021
clc; close all; clear all;
syms x;
g = [1 2 3 4 5]
for i= 1:5
f=g(i)*exp(x)-1; %Enter the Function here
df=diff(f); %The Derivative of the Function
y = newtonraphson(x,f, df, 5, 0.001);
fprintf('%.4f', y);
end
function y = newtonraphson(x, f, df, n, x0)
rounding = 5*10^-(n+1);
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at x0
f0_der=vpa(subs(df,x,x0)); %Calculating the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<rounding %checking the amount of error at each iteration
break;
end
x0=y;
end
y = y - rem(y,10^-n);
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
end
I am learning to use newton raphson method to solve equations.
My function works when g is a single value, say 1 or x. This is without the for loop, for a basic equation.
I added the for loop as I wanted g to have a matrix/array input. Would appreciate the guidance. Thank you!
  2 Comments
Kenneth Chia
Kenneth Chia on 23 Feb 2021
I got this error message. My code is not 1034 long. Only 32.
Am not sure why there is an error with my equation on line 7.

Sign in to comment.

Accepted Answer

Alan Stevens
Alan Stevens on 23 Feb 2021
You could do it like this
f = @(x,g) g.*exp(x) - 1;
df = @(x,g) g.*exp(x);
g = [1 2 3 4 5];
for i= 1:numel(g)
x0 = 0.001;
y = newtonraphson(f, df, g(i), numel(g), x0);
fprintf('%.4f \n', y);
end
function y = newtonraphson(f, df, g, n, x0)
rounding = 5*10^-(n+1);
for i=1:100
f0 = f(x0,g); %Calculating the value of function at x0
f0_der=df(x0,g); %Calculating the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<rounding %checking the amount of error at each iteration
break;
end
x0=y;
end
y = y - rem(y,10^-n);
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
end
  1 Comment
Kenneth Chia
Kenneth Chia on 24 Feb 2021
Thank you! It works!
I also solved my problem too! I just needed a new variable to store my computed values.
syms x;
g = [1 2 3 4 5];
mat = [];
for i= 1:5
f=g(i)*exp(x)-1; %Enter the Function here
df=diff(f); %The Derivative of the Function
y = newtonraphson(x,f, df, 5, 0.001);
mat(i) = y;
fprintf('%.4f', mat(i));
end
function y = newtonraphson(x, f, df, n, x0)
rounding = 5*10^-(n+1);
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at x0
f0_der=vpa(subs(df,x,x0)); %Calculating the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<rounding %checking the amount of error at each iteration
break;
end
x0=y;
end
y = y - rem(y,10^-n);
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!