HOW DO I DO THAT IN MATLAB SERIES?

In this series, I am answering questions that students have asked me about MATLAB.

Contents

TOPIC

How to solve nonlinear equations in MATLAB

SUMMARY

Language : Matlab 2008a; Authors : Sri Harsha Garapati, Daniel Miller, Autar Kaw; Mfile available at Last Revised : January 17, 2012; Abstract: This program shows how to solve a nonlinear equations.

clc
clear all

INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you an how to solve nonlinear equations')

disp(' ')
disp('AUTHOR')
disp('   Sri Harsha Garapati, Daniel Miller and')
disp('Autar K Kaw of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/nonlinear_equation_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 17, 2012')
disp(' ')
ABSTRACT
   This program shows you an how to solve nonlinear equations
 
AUTHOR
   Sri Harsha Garapati, Daniel Miller and
Autar K Kaw of http://autarkaw.wordpress.com
 
MFILE SOURCE
   http://numericalmethods.eng.usf.edu/blog/nonlinear_equation_blog.m
 
LAST REVISED
   January 17, 2012
 

INPUTS

syms x
% nonlinear equation to be solved
eqn='3*x^3-15*x^2+47*x=33';

DISPLAYING INPUTS

disp('INPUTS')
fprintf('  The equation to solve is: %s\n\n',char(eqn))
INPUTS
  The equation to solve is: 3*x^3-15*x^2+47*x=33

THE CODE

using the solve command

soln=solve(eqn,x);
% converting the exact answer to decimal format using double
soln=double(soln);

DISPLAYING OUTPUTS

disp('  ')
disp('OUTPUTS')
disp('  The complete solution to the equation is:')
disp(soln)
  
OUTPUTS
  The complete solution to the equation is:
   0.9244          
   2.0378 - 2.7833i
   2.0378 + 2.7833i