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

  • Context: MATLAB 
  • Thread starter Thread starter ray_sitf
  • Start date Start date
  • Tags Tags
    Matlab Roots
Click For Summary
SUMMARY

This discussion focuses on using MATLAB to find the roots of a quadratic equation using the Bisection method. The provided code requires users to define a function file, specifically 'f.m', to represent the quadratic equation. The example given is for the equation x^2 - 4x - 11 = 0, which translates to the iterative formula x = (4x + 11)^(0.5). Users must input the lower and upper endpoints, tolerance, and maximum iterations to execute the algorithm successfully.

PREREQUISITES
  • Familiarity with MATLAB programming environment
  • Understanding of the Bisection method for root-finding
  • Knowledge of quadratic equations and their standard form
  • Ability to create and use function files in MATLAB
NEXT STEPS
  • Learn how to define and use function files in MATLAB
  • Explore the implementation of the Bisection method in MATLAB
  • Study how to handle user input and output in MATLAB scripts
  • Investigate other root-finding algorithms such as Newton-Raphson in MATLAB
USEFUL FOR

Students, educators, and engineers who are learning MATLAB for numerical methods, particularly those interested in solving quadratic equations and understanding iterative techniques.

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 4 ·
Replies
4
Views
7K
  • · 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
4K
  • · Replies 9 ·
Replies
9
Views
2K