Matlab newton-rapson

  • MATLAB
  • Thread starter arikrubin
  • Start date
  • #1
2
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
 

Answers and Replies

  • #2
jhae2.718
Gold Member
1,161
20
Use element-by-element operators, e.g.:
Code:
f1=x.^3-10*x.^2+15*x+3;
(Periods in front of operators.)
 
  • #3
2
0
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
 

Related Threads on Matlab newton-rapson

  • Last Post
Replies
1
Views
8K
Replies
0
Views
2K
Replies
2
Views
5K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
7
Views
8K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
5
Views
2K
Top