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

In summary, the person is trying to use MATLAB to find the roots of a quadratic equation, but they don't know how to enter the equation or where to tell the computer to start solving it. They are given code to use, but they need to first define a function file before running the code.
  • #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.
 
Physics news on Phys.org
  • #2
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:
[COLOR="Blue"]function[/COLOR] [ y ] = f( x )
    y = a.*x.^2 + b.*x + c; [COLOR="SeaGreen"]% replace a, b, and c with the constants of your quadratic.[/COLOR]
[COLOR="blue"]end[/COLOR]
 

What is matlab and how is it used to find roots?

Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering fields. It has built-in functions and tools for solving mathematical problems, including finding roots of equations.

What is the syntax for finding roots using matlab?

To find the roots of an equation using matlab, the syntax is:
[x, fval] = fzero(fun, x0)
where fun is the equation or function, and x0 is the initial guess for the root.

What are some common errors encountered when finding roots in matlab?

Some common errors when finding roots in matlab include:
- Not providing a valid initial guess for the root
- The function fun not being defined or not returning a numerical output
- The function fun not having a root within the given interval
- The function fun having multiple roots within the given interval
- The function fun being discontinuous within the given interval

Can matlab find complex roots?

Yes, matlab can find complex roots of equations. When using the fzero function, the output x will be a complex number if the root is complex. Additionally, matlab has other functions such as roots and polyval that can also find complex roots.

What are some alternative methods for finding roots in matlab?

Some alternative methods for finding roots in matlab include:
- Using the roots function to find the roots of a polynomial equation
- Using the fsolve function to find roots of a system of equations
- Using the fminbnd function to find the minimum of a function, which can be used to find roots by setting the function to be the squared difference between the equation and 0
- Using the fminsearch function to find the minimum of a function, which can also be used to find roots by setting the function to be the squared difference between the equation and 0

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
989
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Back
Top