MATLAB Solving Root Problem with Matlab Newton-Raphson Method

  • Thread starter Thread starter arikrubin
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion revolves around a coding issue in a MATLAB function designed to find the root of a cubic equation. The user encounters an error related to matrix operations when executing the function. Initially, the error message indicates that the inputs must be a scalar and a square matrix, prompting a suggestion to use element-wise operators (with periods) for calculations. After implementing this change, the user still faces issues, receiving an empty matrix as output when running the function. The user seeks further assistance to resolve the problem and successfully find the root of the equation. The conversation highlights common pitfalls in MATLAB programming, particularly with matrix versus scalar operations and the importance of using the correct syntax for element-wise calculations.
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
Views
1K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
10
Views
3K
Replies
2
Views
3K
Back
Top