MATLAB What is the code for using MATLAB to find the roots of a quadratic equation?

  • Thread starter Thread starter ray_sitf
  • Start date Start date
  • Tags Tags
    Matlab Roots
Click For Summary
To find the roots of a quadratic equation using MATLAB and the provided bisection method code, it is essential to define a function that represents the equation to be solved. The example equation x^2 - 4x - 11 = 0 can be transformed into a fixed-point iteration format, specifically x = (4x + 11)^(0.5). The code requires a separate function file named f.m, where the quadratic equation is defined. Users should replace the coefficients in the function definition with the specific values from their equation. The bisection method code gathers input for the lower and upper endpoints, tolerance, and maximum iterations, then iteratively narrows down the interval to find the root. If the function f is not defined, running the code will result in an error.
ray_sitf
Messages
1
Reaction score
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.
 
Physics news on Phys.org
It looks like you need to define a function file f.m for that code to use. If you run the code in your post as is, I suspect you will receive an error stating that the function f is not defined.

Try something like:
Code:
function [ y ] = f( x )
    y = a.*x.^2 + b.*x + c; % replace a, b, and c with the constants of your quadratic.
end
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
7
Views
9K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
11K