# Matlab newton-rapson

• MATLAB
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

jhae2.718
Gold Member
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