helpmepls
helpmepls

Reputation: 81

Newton–Raphson method with vector inputs in MATLAB

For this problem, we need to make use of the Newton-Raphson method to locate the roots of a particular function. The code works for when the input is a single value, yet when the input is a vector, the answers aren't quite right.
For example, when x=2 is an input, the value 2.5933 is returned and when x=4, 4.3215 is returned. Both these answers are correct, yet when I enter the vector x = [2,4], it returns [2.4106,4.4106].


f = @(x) [(17/77196).*x.^(3)-(15/12866).*x.^(2)+0.004];
fd = @(x) [(17/25732).*x.^(2)-(15/6433).*x];
x= %initial guess;
for i=1:10
x=x-f(x)/fd(x);
end

Upvotes: 2

Views: 268

Answers (1)

Reza
Reza

Reputation: 2025

You can try this

f = @(x) [(17/77196).*x.^(3)-(15/12866).*x.^(2)+0.004];
fd = @(x) [(17/25732).*x.^(2)-(15/6433).*x];
x= [2, 4];
for i = 1:10
    x = x - f(x)./fd(x);
end
x

You were missing . after f(x) to make it an element-wise division.

Upvotes: 2

Related Questions