## How do I do that in MATLAB

HOW DO I DO THAT IN MATLAB SERIES?

## How to PUBLISH in MATLAB

This is how you can get your mfile published.

Click anywhere in the mfile (if your cursor is not in the mfile, you may not get the proper menu open)

-> Go to PUBLISH
-> Click on Down Black Arrow under Publish
-> Choose Edit Publishing Option
-> click on Output file format and the right column entry will show html, but if you click on html, you will get a drop-down box as shown in the figure below.  Choose your option.

You can click on the figure below to see a larger version of the figure.

This post is brought to you by

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

## Ability to break long fprintf statements

Problem:
A student sent me an email – ” I can’t figure out how to continue my text on another line in the editor while using the fprintf command in MATLAB.  It seems like this should be something simple, but I can’t figure it out.  The closest thing I have tried is:
fprintf(‘filler filler filler’, …
‘filler filler’)

When I run this script, this only displays the content within the first set of quotes though.

Solution:
Assign the long strings to variables, and then use the fprintf statements

abc=’My name is’
def=’ Slim Shady, also known as Marshall Mathers’
fprintf([abc def])

or you could do the following, especially if you have other non-string variables to print

abc=’My name is’
def=’ Slim Shady, also known as Marshall Mathers’
fprintf(‘%s %s’,abc,def)

_____________________

This post is brought to you by

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

## How do I solve simultaneous linear equations given in equation form? Updated 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 simultaneous linear equations given in equation form.

• 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/SimultaneousEquationsEqForm.m;
% Last Revised : January 22, 2020;
% Abstract: This program shows you how to solve a set of
% simultaneous linear equations given in equation form?
% .
clc
clear all
%% INTRODUCTION
disp(‘ABSTRACT’)
disp(‘ This program shows you how to solve a’)
disp(‘ set of simultaneous linear equations given in equation form’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘ Autar Kaw and Luis Serrano’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘ http://nm.mathforcollege.com/blog/SimultaneousEquationsEqForm.m&#8217;)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘ January 22, 2020’)
disp(‘ ‘)
%% INPUTS
% Enter the equations
syms a b c
eqn1=12*a+23*b+39*c==29
eqn2=13*a+17*b+19*c==37
eqn3=21*a+23*b+29*c==59
%% DISPLAYING INPUTS
disp(‘ ‘)
disp(‘INPUTS’)
disp(‘________________________’)
disp(‘Equations’)
disp(‘________________________’)
disp(eqn1)
disp(eqn2)
disp(eqn3)
%% THE CODE
% The solution
X=solve(eqn1,eqn2,eqn3);
% Assigning the output
a=double(X.a);
b=double(X.b);
c=double(X.c);
%% DISPLAYING OUTPUTS
disp(‘ ‘)
disp(‘OUTPUTS’)
disp(‘________________________’)
disp(‘Solution Vector’)
disp(‘________________________’)
fprintf(‘\nValue of a= %g’,a)
fprintf(‘\nValue of b= %g’,b)
fprintf(‘\nValue of c= %g’,c)
disp(‘ ‘)
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

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

## Reducing ordinary differential equations to state variable matrix form

To be able to solve differential equations numerically, one has to reduce them to a set of first order ordinary differential equations – also called the state variable form.  By writing them in a matrix form, the equations become conducive for programming in languages such as MATLAB.  Here is an example of this reduction to state variable matrix form.  This post is brought to you by

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

## Unexpected zeros error in MATLAB in zeros function

YouTube Comment: This MATLAB program gives me an error.  W=2.4; L=3; Delta=0.6; i=(W./Delta)+2; j=(L./Delta); T=zeros(i,j); . When I write 0.1 for Delta, there is an error for zeros statement.

Answer: When writing a new program, avoid using the semicolon as it suppresses the output. Write each line separately in a .m file and run the mfile. That way you would have noticed that “i” is turning out to be a real number. It shows up as 26.0000 but if you use format long statement, you will see that you get 25.999999999999996. You can round(i) and round(j) to the nearest integer. The reason “i” turns out to be 25.999999999999996 is because of roundoff error, as numbers and calculations get represented in binary format.

_________________________________________________________________________

This post is brought to you by

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

## Reading an excel file in MATLAB

Recently I taught a volunteer class to professional engineers on MATLAB.  Two of the most requested items of interest were
1. How do I read an excel file?
2. How do I do curve fitting?

We address the first question here.  It is easy to read an excel file with the xlsread command but what do you with it once the file has been assigned.  So we took a simple example of an excel spreadsheet where the first column consists of a student number and the second column has the examination scores of the students.  You are asked to find the highest score.

The MATLAB program link is here.
The HTML version of the MATLAB program is here.
The Excel file used in the MATLAB program is here

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.

% Language : Matlab 2008a
% Authors : Autar Kaw
% Last Revised : December 12, 2010
% Abstract: This program shows you how to read an excel file in MATLAB
% The example has student numbers in first column and their score in the
% second column
clc
clear all
disp(‘This program shows how to read an excel file in MATLAB’)
disp(‘Matlab 2008a’)
disp(‘Authors : Autar Kaw’)
disp(‘Last Revised : December 12, 2010’)
disp(‘http://nm.mathforcollege.com&#8217;)
disp(‘  ‘)

%% INPUTS
% We have two column data and it has headers in the first row.
% That is why we read the data from A2 to B32.
disp(A)
disp(‘  ‘)
%% SOLUTION
% Finding the number of rows and columns
sizem=size(A);
rows_A=sizem(1);
cols_A=sizem(2);
% Assigning the scores to a vector called score
for i=1:1:rows_A
score(i)=A(i,2);
end
% Using the max command to find the maximum score
% HW: Write your own function “max”
maxscore=max(score);
% Finding which student got the highest score
for i=1:1:rows_A
if score(i)==maxscore
student_no=i;
break;
end
% HW: What if more than one student scored the highest grade??
end
%% OUTPUT
disp(‘  ‘)
disp (‘OUTPUT’)
fprintf(‘Student Number# %g scored the maximum score of %g’,…
student_no,maxscore)
disp(‘ ‘)

This post is brought to you by

## A short online quiz for the MATLAB conditional statements

Frequent testing has been proven to be effective in learning.  Here we have an online quiz on MATLAB conditional statements (only if-then-else), where some of the questions are calculated (meaning that the numbers in these questions change when you retake the quiz).  So give it a try.
http://numericalmethods.eng.usf.edu/EML3041/studymate/MATLABifelseend.htm

_____________________________________________________

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