Q BgQuestion:

Scholar
Karma Points: 200
Respect (100%):
posted by  MyMatlabSucks on 10/1/2008 12:35:14 AM  |  status: Closed  

Matlab programming

Course Textbook Chapter Problem
Other N/A N/A N/A
Question Details:
Consider the cubic equation ax^3 + bx^2 + cx +d =0
where a doesn't equal to 0, b,c,d constant.
Solve the equation numerically, for each of the bisection method, newton's method, Muller's method:
 
a) First compute a root of the equation:
b) Use deflation procedure to reduce equation to a second order equation
c) Solve the second order equation
 
write a matlab functions that perform the above calculation for each method. Each function should take as input a column vector [ a b c d]T and output the roots of the polynomial in the form of a column vector.
Tags: Other, Other
Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

AAnswers:

Answer Question
Oracle
Karma Points: 21,689
(Adnan Menderes University)
posted by velixy on 10/3/2008 10:19:53 AM  |  status: Live
Asker's Rating: Lifesaver   
MyMatlabSucks's comment:
"Although a lit late but thanks a lot! lifesaver!"
Response Details:

function yt= f(x)

a=1;

b=3;

c=3;

d=1;

yt= a*x^3 + b*x^2+c*x+d;


 
% bisection method

clear; % clear memory

% Initial interval is [a,b]; tol is tolerance

a = input('Enter value of a: ');

b = input('Enter value of b: ');

tol = input('Enter vlaue of tolerance: ');

maxIts=20; % maximum number of Iterations

% Check that a,b are good

if f(a)*f(b) > 0 ,

disp('f(a)*f(b) > 0; cannot guarantee a zero in [a,b]');

return; % terminate and return to prompt

end;

%

disp(' Left Right f(mid)') % Print column headings

% Set up plotting

N=100; %

x=a:(b-a)/(N-1):b; y=zeros(size(x));

for (i=1:N); y(i)=f(x(i)); end;

min=min(y); max=max(y);

hold off;

plot(x, y,'r-');

hold on;

plot([a,b],[0,0],'r-');

xlabel('x'); ylabel('f(x)'); title('Bisection algorithm');

 

% Start bisection

left = a; right = b; fleft = f(left);

n = 0; % counter

 

while n <= maxIts,

if abs(right - left) < tol

break

end; % iteration completed

mid = left + (right - left)/2; fmid = f(mid);

% mark old interval green

plot([left,left], [min,max],'g-');

plot([right,right], [min,max],'g-');

% update

if (fmid*fleft > 0 )

left = mid;

else

right = mid;

end;

% Results to screen.

disp(sprintf('%12.6e %12.6e %12.6e', left, right, fmid))

n = n + 1; % increase counter

% mark new interval blue

plot([left,left], [min,max],'b-');

plot([right,right], [min,max],'b-');

disp('Hit any key');

pause;

end;

if n > maxIts,

disp('No convergence to requested accuracy')

else

disp('Succesfully computed root');

alpha=mid

end;

hold off;

 
ANSWER
 
  -0.9996
 
 
newton  method
 
 

x(1)= -0.5;

a=1;

b=3;

c=3;

d=1;

n=15;

disp(' x(i+1)-x(i) x_n')

disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

for i=1:(n + 1 )

x(i+1) = x(i) -( a*(x(i))^3+b*(x(i))^2 +c*x(i) +d)/...

( 3*a*(x(i))^2+ 2*b*(x(i)) +c );

if rem(i-1 ,1) ==0

fprintf('%15.8f%19.7f\n',x(i)-x(i+1) ,x(i) );

end

end

 
>> newton101
      x(i+1)-x(i)           x_n
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     0.16666667         -0.5000000
     0.11111111         -0.6666667
     0.07407407         -0.7777778
     0.04938272         -0.8518519
     0.03292181         -0.9012346
     0.02194787         -0.9341564
     0.01463192         -0.9561043
     0.00975461         -0.9707362
     0.00650307         -0.9804908
     0.00433538         -0.9869939
     0.00289025         -0.9913292
     0.00192684         -0.9942195
     0.00128456         -0.9961463
     0.00085637         -0.9974309
     0.00057091         -0.9982873
     0.00038061         -0.9988582
 
 
 
 

 
 
 
ADU
Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different than tutoring »