Solving Root Problem with Matlab Newton-Raphson Method

  • Context: MATLAB 
  • Thread starter Thread starter arikrubin
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on resolving an issue with the implementation of the Newton-Raphson method in MATLAB for finding roots of the polynomial function \(f(x) = x^3 - 10x^2 + 15x + 3\). The user encountered an error related to matrix operations, specifically "Inputs must be a scalar and a square matrix." The solution provided involves using element-wise operators (e.g., `.^` and `.*`) to ensure proper computation with arrays. After applying these changes, the user still faced an issue with an empty matrix output, indicating further debugging is required to ensure the function converges to a root.

PREREQUISITES
  • Understanding of the Newton-Raphson method for root finding
  • Familiarity with MATLAB syntax and functions
  • Knowledge of polynomial functions and their derivatives
  • Experience with element-wise operations in MATLAB
NEXT STEPS
  • Review MATLAB documentation on element-wise operations
  • Learn about debugging techniques in MATLAB to identify issues in function outputs
  • Explore the convergence criteria for the Newton-Raphson method
  • Investigate alternative root-finding methods in MATLAB, such as `fzero`
USEFUL FOR

Mathematics students, MATLAB programmers, and engineers working on numerical methods for root finding will benefit from this discussion.

arikrubin
Messages
2
Reaction score
0
hey
i have a question for u guys.
i have wrriten this code:

function[a]=search_root_3(x,e)
f1=x^3-10*x^2+15*x+3;
f11=diff(x^3-10*x^2+15*x+3,x,1);
if abs(f1)<=e
fprintf('the root is %d% \n');
return
end
while abs(f1)>=e
x=x-(f1/f11);
f1=x^3-10*x^2+15*x+3;
f11=diff(x^3-10*x^2+15*x+3,x,1);
end
a=x;
end
the goal is to find the root.
when i am trying to run this code i getting the next eror even though i have used f1 (exact form) in other codes seccessfully:
>> [a]=search_root_3(x1,e)
? Error using ==> mpower
Inputs must be a scalar and a square matrix.

Error in ==> search_root_3 at 10
f1=x^3-10*x^2+15*x+3;

i will be glad to get some help
thanx
arik
 
Physics news on Phys.org
Use element-by-element operators, e.g.:
Code:
f1=x.^3-10*x.^2+15*x+3;
(Periods in front of operators.)
 
hey thanx!
but now, even though i have added the dot tha mathlab still thinks that it is a mtrix.
this is the code:

function[a]=search_root_3(x,e)
f1=x.^3-10*x.^2+15*x+3;
f11=diff(x.^3-10*x.^2+15*x+3,x,1);
if abs(f1)<=e
fprintf('the root is %d% \n');
return
end
while abs(f1)>=e
x=x-(f1/f11);
f1=x.^3-10*x.^2+15*x+3;
f11=diff(x.^3-10*x.^2+15*x+3,x,1);
end
a=x;
end

and this is what is says after trying to run the code

>> a=search_root_3(x1,e)

a =

Empty matrix: 1-by-0


and i am asking YYYY?!
i will be gald for some help
thank u all
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K