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

30 ビュー (過去 30 日間)
Kenneth Chia
Kenneth Chia 2021 年 2 月 23 日
編集済み: Kenneth Chia 2021 年 2 月 24 日
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 件のコメント
Kenneth Chia
Kenneth Chia 2021 年 2 月 23 日
Error using symengine
Dimensions do not match.
Error in sym/privBinaryOp (line 1034)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in * (line 329)
X = privBinaryOp(A, B, 'symobj::mtimes');
Error in test_newton (line 7)
f=g*exp(x)-1; %Enter the Function here
Kenneth Chia
Kenneth Chia 2021 年 2 月 23 日
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.

サインインしてコメントする。

採用された回答

Alan Stevens
Alan Stevens 2021 年 2 月 23 日
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 件のコメント
Kenneth Chia
Kenneth Chia 2021 年 2 月 24 日
編集済み: Kenneth Chia 2021 年 2 月 24 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by