Solving a polynomial equation for the longest mast problem?

In the previous post, http://autarkaw.wordpress.com/2010/06/10/a-real-life-example-of-having-to-solve-a-nonlinear-equation-numerically/, we set up the polynomial equation for the problem of finding the length of the mast before which it buckles under its own weight.  In this blog, we show you how the polynomial equation is solved.  Since the polynomial equation has infinite terms, we also show you how we choose how many terms of the polynomial need to be used.

It is better to download the program as single quotes in the pasted version do not translate properly when pasted into a mfile editor of MATLAB or you can read the html version for clarity and sample output .

%% FINDING THE SMALLEST POSITIVE ROOT OF A POLYNOMIAL EQUATION FOR A
% In a previous blog at autarkaw.wordpress.com (June 10), we set up a
% polynomial equation that would allow us to find the longest mast
% that can be setup before it buckles under its own weight.
% In this blog, we will find the root of this equation.
% The problem is given at
% http://nm.mathforcollege.com/blog/length_of_mast.pdf
% and we are solving Exercise 1 of the pdf file.

%% TOPIC
% Smallest positive root of a polynomial equations with infinite terms

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/rootinfinite.m;
% Last Revised : July 4, 2010
% Abstract: This program shows you how to find the smallest positive
% real root of a polynomial equations with infinite terms
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to’)
disp(‘   find the smallest positive real root’)
disp(‘   of a polynomial equations with infinite terms’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/rootsinfinite.m’)
disp(‘  ‘)
disp(‘PROBLEM STATEMENT’)
disp(‘   http://nm.mathforcollege.com/blog/length_of_mast.pdf)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   July 4, 2010’)
disp(‘ ‘)

%% INPUTS
% prespecified tolerance, eps
eps=0.000001;
% maximum number of terms of polynomial
nmax=100;

%% DISPLAYING INPUTS

disp(‘INPUTS’)
fprintf(‘ The max number of terms of the polynomial chosen, nmax= %g’,nmax)
fprintf(‘\n The prespecified tolerance, eps= %g’,eps)
disp(‘  ‘)
disp(‘  ‘)

%% CODE
for N=2:1:nmax
    % The above looop is to see how many terms we should take of the
    % infinite polynomial
  aa(1)=-3.0/8.0;
    % Setting up the polynomial via recursive relations
for i=2:1:N
  aa(i)=-3*aa(i-1)/(4*i*(3*i-1));
end
% Since it is a polynomial of order N,
% there are N+1 coefficients
% To set up the polynomial for MATLAB
% N+1 th coefficient is the constant term
% N th coefficient is the term of order 1
% and so on till 1st coefficient is of order N
bb(N+1)=1;
for i=1:1:N
    bb(N-i+1)=aa(i);
end

% Finding all the roots of the Nth order polynomial
abc=roots(bb);

% Finding the first real positive root so that it
% would be used as the starting minimum value available
for i=1:1:N
    if isreal(abc(i))==true & abc(i)>0
        minval=abc(i);
        break;
    end
end

% Finding the smallest positive real root

for i=1:1:N
    if isreal(abc(i))==true & abc(i)>0
        if (abc(i) < minval)
            minval=abc(i);
        end
    end
end
% Checking if prespecified tolerance is met
if N>2
    absea=abs((minval-previous)/minval)*100;
    if absea<=eps
        terms_needed=N;
        break;
    end
end
previous=minval;
end

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
fprintf(‘ The number of terms used in the polynomial is %g’,terms_needed)
fprintf(‘\n The smallest positive real root is %g’,minval)
disp(‘  ‘)

________________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://www.youtube.com/numericalmethodsguy.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

A real-life example of having to solve a nonlinear equation numerically?

_______________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

How do I solve a nonlinear equation that needs to be setup in MATLAB?

Many students ask me how do I do this or that in MATLAB. So I thought why not have a small series of my next few blogs do that. In this blog, I show you how to solve a nonlinear equation that needs to be set up.

For example to find the depth ‘x’ to which a ball is floating in water is based on the following cubic equation
4*R^3*S=3*x^2*(R-x/3)
where
R= radius of ball
S= specific gravity of ball
So how do we set this up if S and R are input values?

The MATLAB program link is here.

The HTML version of the MATLAB program is here.

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I solve a nonlinear equation if I need to set it up?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/integration.m;
% Last Revised : March 28, 2009;
% Abstract: This program shows you how to solve a nonlinear equation
% that needs to set up as opposed that is just given to you.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to solve’)
disp(‘   a nonlinear equation that needs to be setup’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/nonlinearequation.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   April 17, 2009’)
disp(‘ ‘)

%% INPUTS
% Solve the nonlinear equation where you need to set up the equation
% For example to find the depth ‘x’ to which a ball is floating in water
% is based on the following cubic equation
% 4*R^3*S=3*x^2*(R-x/3)
% R= radius of ball
% S= specific gravity of ball
% So how do we set this up if S and R are input values

S=0.6
R=0.055
%% DISPLAYING INPUTS
disp(‘INPUTS’)
func=[‘  The equation to be solved is 4*R^3*S=3*x^2*(R-x/3)’];
disp(func)
disp(‘  ‘)

%% THE CODE
% Define x as a symbol
syms x
% Setting up the equation
C1=4*R^3*S
C2=3
f=[num2str(C1) ‘-3*x^2*(‘ num2str(R) ‘-x/3)’]
% Finding the solution of the nonlinear equation
soln=solve(f,x);
solnvalue=double(soln);

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
for i=1:1:length(solnvalue)
fprintf(‘\nThe solution# %g is %g’,i,solnvalue(i))
end
disp(‘  ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

How do I solve a nonlinear equation in MATLAB?

Many students ask me how do I do this or that in MATLAB.  So I thought why not have a small series of my next few blogs do that.  In this blog, I show you how to solve a nonlinear equation.

The MATLAB program link is here.

The HTML version of the MATLAB program is here.

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I solve a nonlinear equation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/integration.m;
% Last Revised : March 28, 2009;
% Abstract: This program shows you how to solve a nonlinear equation.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to solve’)
disp(‘   a nonlinear equation’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/nonlinearequation.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   April 11, 2009’)
disp(‘ ‘)

%% INPUTS
% Solve the nonlinear equation x^3-15*x^2+47*x-33=0
% Define x as a symbol
syms x
% Assigning the fleft hand side o the equation f(x)=0
f=x^3-15*x^2+47*x-33;
%% DISPLAYING INPUTS

disp(‘INPUTS’)
func=[‘  The equation to be solved is ‘ char(f), ‘=0’];
disp(func)
disp(‘  ‘)

%% THE CODE

% Finding the solution of the nonlinear equation
soln=solve(f,x);
solnvalue=double(soln);

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
for i=1:1:length(solnvalue)
fprintf(‘\nThe solution# %g is %g’,i,solnvalue(i))
end
disp(‘  ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.