## How do I do that in MATLAB

HOW DO I DO THAT IN MATLAB SERIES?

## How do I solve a nonlinear equation that needs to be setup – Updated to MATLAB 2018b

Guest co-blogger: Luis Serrano

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
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.

• DO NOT COPY AND PASTE THE PROGRAM BELOW BECAUSE THE SINGLE QUOTES DO NOT TRANSLATE TO THE CORRECT SINGLE QUOTES IN MATLAB EDITOR.  DOWNLOAD THE MATLAB PROGRAM INSTEAD

%% SUMMARY
% Language : Matlab 2018b;
% Authors : Autar Kaw and Luis Serrano;
% Mfile available at
% http://nm.mathforcollege.com/blog/NonlinearEquations_withSetUp.m;
% Last Revised : January 22, 2020;
% 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(‘AUTHORS’)
disp(‘ Autar Kaw and Luis Serrano’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘ http://nm.mathforcollege.com/blog/NonlinearEquations_withSetUp.m&#8217;)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘ January 22, 2020’)
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*x^2*(R-x/3)
f=C1==C2
% Finding the solution of the nonlinear equation
soln=vpasolve(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 Open Course Ware:
• the textbooks on
• the Massive Open Online Course (MOOCs) available at

## A MATHCOUNTS problem solution via abstraction  _________________

This post is brought to you by

Holistic Numerical Methods Open Course Ware:

the textbooks on

the Massive Open Online Course (MOOCs) available at

## Prerequisite Example for Newton Raphson Method  ____________________________________________________

This post is brought to you by

• Holistic Numerical Methods Open Course Ware:
• the textbooks on
• the Massive Open Online Course (MOOCs) available at

## Background Example for Newton Raphson Method

One of the three tenets of a student succeeding in a course is how well he knows the pre-requisite knowledge for the course (other two tenets are ability and interest).  We as instructors can make it easier for students to get to a reasonable competency level by offering short reviews of the pre-requisite knowledge in form of video and/or text.

Here , a student will review via an example the background needed to learn Newton-Raphson method of solving a nonlinear equation of the form f(x)=0.

For more videos and resources on this topic, please visit http://nm.mathforcollege.com/topics/newton_raphson.html

________________________

This post is brought to you by

• Holistic Numerical Methods Open Course Ware:
• the textbooks on
• the Massive Open Online Course (MOOCs) available at

## Rejecting roots of nonlinear equation for a physical problem?

How do we reject roots of a nonlinear equation for a physical problem?

To make the fulcrum of a bascule bridge, a long hollow steel shaft called the trunnion is shrink fit into a steel hub. The resulting steel trunnion-hub assembly is then shrink fit into the girder of the bridge. This is done by first immersing the trunnion in a cold medium such as dry-ice/alcohol mixture.  After the trunnion reaches the steady state temperature of the cold medium, the trunnion outer diameter contracts.  The trunnion is taken out of the medium and slid though the hole of the hub.   When the trunnion heats up, it expands and creates an interference fit with the hub. In 1995, on one of the bridges in USA, this assembly procedure did not work as designed.  Before the trunnion could be inserted fully into the hub, the trunnion got stuck.  So a new trunnion and hub had to be ordered at a cost of \$50,000.  Coupled with construction delays, the total loss was more than hundred thousand dollars.

Why did the trunnion get stuck?  This was because the trunnion had not contracted enough to slide through the hole.

Now the same designer is working on making the fulcrum for another bascule bridge.  Can you help him so that he does not make the same mistake?

For this new bridge, he needs to fit a hollow trunnion of outside diameter  in a hub of inner diameter .  His plan is to put the trunnion in dry ice/alcohol mixture (temperature of the fluid – dry ice/alcohol mixture is -108 degrees F) to contract the trunnion so that it can be slided through the hole of the hub.  To slide the trunnion without sticking, he has also specified a diametrical clearance of at least 0.01 inches  between the trunnion and the hub.   What temperature does he need to cool the trunnion to so that he gets the desired contraction?

This post is brought to you by

• Holistic Numerical Methods Open Course Ware:
• the textbooks on
• the Massive Open Online Course (MOOCs) available at

## Does the solve command in MATLAB not give you an answer?

In a recent blog about the MATLAB solve command, I mentioned that the solve command was not giving us the unique real solution to a physical problem.  A user at MATLAB suggested declaring the syms variable as real, before using the solve command. That worked.  So if the equation in terms of the variable b is eqnb=0, then use

syms b real;
solve (eqnb,b);

____________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, the textbook on Introduction to Programming Concepts Using MATLAB, and the YouTube video lectures available athttp://numericalmethods.eng.usf.edu/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

## A Wolfram demo on how much of a floating ball is under water

Here is another Wolfram demo. This one shows how much of a floating ball would be submerged under water.  The demonstration uses the specific gravity and radius of the ball as inputs.  Using calculus and Archimedes’ principle, the level to which the ball is submerged turns out to be the solution of a cubic equation.  To play with the demo, download the free CDF player from Wolfram first.

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, the textbook on Introduction to Programming Concepts Using MATLAB, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

## Using int and solve to find inverse error function in MATLAB

In the previous post, https://autarkaw.wordpress.com/2010/08/24/finding-the-inverse-error-function/, we set up the nonlinear equation to find the inverse of error function.  Using the int and solve MATLAB commands, we write our own program to find the inverse error function.

It is better to download (right click and save target) 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 INVERSE ERROR FUNCTION
% In a previous blog at autarkaw.wordpress.com (August 24, 2010),
% we set up a nonlinear equation to find the inverse error function.
% In this blog, we will solve this equation.
% The problem is given at
% http://nm.mathforcollege.com/blog/inverseerror.pdf
% and we are solving Exercise 1 of the pdf file.

%% TOPIC
% Finding inverse error function

%% SUMMARY

% Language : Matlab 2010a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/inverse_erf_matlab.m;
% Last Revised : August 27, 2010
% Abstract: This program shows you how to find the inverse error function
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to’)
disp(‘   find the inverse error function’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/inverse_erf_matlab.m’)
disp(‘  ‘)
disp(‘PROBLEM STATEMENT’)
disp(‘   http://nm.mathforcollege.com/blog/inverseerror.pdf’)
disp(‘        Exercise 1’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   August 27, 2010’)
disp(‘ ‘)

%% INPUTS
% Value of error function
erfx=0.5;

%% DISPLAYING INPUTS

disp(‘INPUTS’)
fprintf(‘ The value of error function= %g’,erfx)
disp(‘  ‘)
disp(‘  ‘)

%% CODE
syms t x
inverse_erf=solve(int(2/sqrt(pi)*exp(-t^2),t,0,x)-erfx);
inverse_erf=double(inverse_erf);
%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
fprintf(‘ Value of inverse error function from mfile is= %g’,inverse_erf)
fprintf(‘\n Value of inverse error function using erfinv is= %g’,erfinv(erfx))
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,
the textbook on Introduction to Programming Concepts Using MATLAB, and
the YouTube video lectures available at http://nm.mathforcollege.com/videos 