- #1
ray_sitf
- 1
- 0
I am trying to use MATLAB to find the roots of a quadratic by the standard iterative techniques. I am totally on top of all this work in theory and in practice when it comes to doing it with a calculator or Excel, but I have never used MATLAB before and I have been given the code below to use.
The trouble is I don't know how I am s'posed to enter the equation that I am using for the iteration.
Suppose the equation was x^2 - 4x -11 = 0
Then I obviously need to get the computer to recognise that I want to find the fixed point of
x = (4x+11)^.5
But where do I tell the computer that this is the equation I want to solve?
Here is the code I was given. I suppose it must have something to do with "Gather input data"?
% programme Bisection
% Bisection method for finding root of equation
% Algorithm from Burden & Faires page 47
%
% Gather input data
a = input('Input lower endpoint: ');
b = input('Input upper endpoint: ');
tol = input('Input tolerance: ');
Nmax = input('Input maximum number of iterations: ');
count = 1;
fa = f(a);
% Iterate
while count <= Nmax
p = a + (b-a)/2;
fp = f(p);
% Test for finish
if or(fp == 0, (b-a)/2 < tol)
disp('Calculated root is: ');
disp(p);
disp('Number of iterations was: ');
disp(count);
return;
end
count = count + 1;
% Compute next interval
if fa*fp > 0
a = p;
fa = fp;
else
b=p;
end
end
% If we get here then no root was found
disp('Method failed: Number of iterations: ')
disp(count);
Thankyou very much in advance.
The trouble is I don't know how I am s'posed to enter the equation that I am using for the iteration.
Suppose the equation was x^2 - 4x -11 = 0
Then I obviously need to get the computer to recognise that I want to find the fixed point of
x = (4x+11)^.5
But where do I tell the computer that this is the equation I want to solve?
Here is the code I was given. I suppose it must have something to do with "Gather input data"?
% programme Bisection
% Bisection method for finding root of equation
% Algorithm from Burden & Faires page 47
%
% Gather input data
a = input('Input lower endpoint: ');
b = input('Input upper endpoint: ');
tol = input('Input tolerance: ');
Nmax = input('Input maximum number of iterations: ');
count = 1;
fa = f(a);
% Iterate
while count <= Nmax
p = a + (b-a)/2;
fp = f(p);
% Test for finish
if or(fp == 0, (b-a)/2 < tol)
disp('Calculated root is: ');
disp(p);
disp('Number of iterations was: ');
disp(count);
return;
end
count = count + 1;
% Compute next interval
if fa*fp > 0
a = p;
fa = fp;
else
b=p;
end
end
% If we get here then no root was found
disp('Method failed: Number of iterations: ')
disp(count);
Thankyou very much in advance.