9 views (last 30 days)

Show older comments

Kenneth Chia on 23 Feb 2021

Edited: Kenneth Chia on 24 Feb 2021

Accepted Answer: Alan Stevens

Open in MATLAB Online

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 Show NoneHide None

Show NoneHide None

Kenneth Chia on 23 Feb 2021

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/753414-matlab-newton-raphson-method-with-a-function-with-array-matrix-variables#comment_1347904

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 on 23 Feb 2021

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/753414-matlab-newton-raphson-method-with-a-function-with-array-matrix-variables#comment_1347914

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.

Sign in to answer this question.

### Accepted Answer

Alan Stevens on 23 Feb 2021

Open in MATLAB Online

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 Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Kenneth Chia on 24 Feb 2021

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/753414-matlab-newton-raphson-method-with-a-function-with-array-matrix-variables#comment_1350739

Edited: Kenneth Chia on 24 Feb 2021

Open in MATLAB Online

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)

Sign in to answer this question.

### See Also

### Categories

Mathematics and OptimizationOptimization ToolboxSystems of Nonlinear EquationsNewton-Raphson Method

Find more on **Newton-Raphson Method** in Help Center and File Exchange

### Tags

- matlab function
- newton raphson

### Community Treasure Hunt

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

Start Hunting!

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office