## A short online quiz on MATLAB basics

Frequent testing has been proven to be effective in learning.  Here we have an online quiz on MATLAB basics, 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 and soon we will be adding questions on programming basics.
http://numericalmethods.eng.usf.edu/EML3041/studymate/matlabbasics.htm

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, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos and http://www.youtube.com/numericalmethodsguy

## MATLAB code for bubble sort

In the previous blog, we spelled out the bubble sort algorithm for putting an array of numbers in an ascending order.   In this post, I am posting the matlab program. 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 see the html version for clarity and sample output.

%% PUTTING AN VECTOR OF NUMBERS IN AN ASCENDING ORDER?
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 8, 2009
% Abstract: This program shows you how to put a vector
% of numbers in an ascending order using the bubble sort method
clc
clear all
disp(‘This program shows the bubble sort method’)
disp(‘to put a vector of numbers in an ‘)
disp(‘ascending order’)
disp(‘Matlab 2007a’)
disp(‘Authors : Autar Kaw’)
disp(‘Last Revised : November 8, 2009’)
disp(‘http://numericalmethods.eng.usf.edu&#8217;)
disp(‘  ‘)
%% INPUTS
% The vector of numbers
disp (‘INPUTS’)
disp(‘Input the vector of numbers’)
A=[18  7  6  15  4  13];
disp(A)
%% SOLUTION
% Number of entries, n
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)>A(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end

%% OUTPUT
disp(‘  ‘)
disp (‘OUTPUT’)
disp (‘The ascending matrix is’)
disp(A)

_______________________________________________________

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, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos and http://www.youtube.com/numericalmethodsguy

## Bubble sorting

Last week, I was teaching how to randomly pick lotto numbers using MATLAB.  The problem was that some of the numbers that were getting picked were identical.  We solved this by using comparisons until the current number picked is different from the previously selected numbers.  We blogged on this a few months ago.  But there is still an aesthetic problem of how the numbers are presented.  The numbers are not in an ascending or descending order.  This is a good time to show how to do this using the simplest (if not the most efficient) procedure called the bubble sort.

Let’s suppose someone asks you to put [8 7  9  5   4] in an ascending order.

Starting from the first number, you compare the number with the next number, and see if it is greater.  If it is, you swap the numbers.  You continue to do this with the second number, third number and so on until the second last number.  What this does is bubble the largest number to the end.

[8  7  9  5  4 ] -> [7  8  9  5  4]  (as 8>7) -> [7  8  9  5  4]  (as 8 is not > 9)-> [7  8  5   9   4]  (as 9 is >5) -> [7  8   5  4  9]  (as 9>4).  See how the largest number is at the end.

Now repeat this.  [7  8   5  4  9]  -> [7  8  5  4  9] (as 7 is not >8] -> [7   5  8  4  9] (as 8>5) -> [7  5  4  8 9]  (as 8>4) ->  [7  5  4  8  9 ] (as 8 is not >9)

Now repeat this.  [ 7  5  4  8  9] -> [5  7  4  8  9] -> [ 5  4  7  8  9] -> [ 5  4  7  8  9]  -> [  5  4  7  8  9]

Now repeat this.  [5  4  7  8  9] -> [ 4  5  7  8  9] -> [ 4  5  7  8 9] -> [ 4  5  7  8   9]  -> [  4 5  7  8  9]

It looks like we are done.  If n is the number of the numbers in the array, it takes (n-1) swaps within each of the (n-1) repetitions.  So we do not have to guess how many swaps it takes or how many repetitions it takes.

To make the bubble sort efficient, we can do the following: 1) Since with each repetition the largest number bubbles up, we may need to do less swaps.  For the first repetition, we will do (n-1) swaps, for the next repetition, we do the first (n-2) swaps, and so on.  2) We can also keep track of number of swaps taking place in a repetition.  If no swaps take place in a repetition, no more repetitions are needed.

_______________________________________________________

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, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos and http://www.youtube.com/numericalmethodsguy

## How do I numerically solve an ODE in MATLAB?

The other day a student came to ask me for help in solving a second order ordinary differential equation using the ode45 routine of MATLAB.  To use ode45, one needs to be familiar with how the inputs are required by MATLAB.  The understanding of these inputs is important to use ode45 successfully in problems that are more complex than solving a second order ODE.

The ordinary differential equation was
2y”+3y’+5y=7 exp(-x), y(0)=11, dy/dx(0)=13
This has to put in the state variable form by reducing it by using
y’=z
That gives
y’=z with the corresponding initial conditions as y(0)=11
Then
2y”+3y’+5y=7 exp(-x)
reduces to
2z’ + 3z+5y=7exp(-x)
z’ =(7exp(-x)-3z-5y)/2 with the corresponding initial conditions as z(0)=13

So as needed by MATLAB, call y as y(1) and z as y(2)
dy(1)=y(2), y(1) at x=0 is 11
dy(2)=(7exp(-x)-3y(2)-5y(1))/2, y(2) at x=0 is 13

These equations are now put in a MATLAB function we call odestate.m
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=(7*exp(-x)-3*y(2)-5*y(1))/2;

To solve the ODE, the
The inputs are
1) the function odestate
2) The outputs are required between x=0 and x=17,
hence entered as [0 17]
3) The initial conditions are y(0)=11 and dy/dx(0)=13,
hence entered as [11  13]

The outputs are
1) X= array of x values between 0 and 17
2) Y= matrix of 2 columns;
first column is the y(x)
second column is dy/dx(x)
The MATLAB code then is
[X,Y]=ode45(@odestate,[0  17],[11 13]);

Click the links for the MATLAB mfiles for the function odestate.m and the ODE solver odetest.m

_________________________________________________________________________________

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

## The continue statement in MATLAB

The continue statement in MATLAB is used to pass control to the next iteration in for and while statements.  Let’s suppose someone wants to find and print the value of k^2-50 for all integers in [-10,10] domain.  The mfile for that is given below.

% For integers k=-10,-9,….,9,10,
% the function k^2-50 will take positive as
% well as negative values.
%For example, for k=-9, k^2-50=31; for k=1,
% k^2-50=-49; for k=8, k^2-50=14.
% The loop below will calculate values of k^2-50 for
% all values of requested k.
for k=-10:1:10
val=k^2-50;
fprintf(‘\n k=%g  val=%g’,k,val)
end

___________________________________________________________

Let’s suppose now you are asked to calculate and print value of k^2-50 for all integers in [-10,10] domain but only if (k^2-50) is positive.

% The loop below will calculate and print values of k^2-50
% for all values of the requested k
% for which k^2-50 is positive.
for k=-10:1:10
if (k^2-50<0)
continue;
end
val=k^2-50;
fprintf(‘\n k=%g  val=%g’,k,val)
end

____________________________________________________________

Can you do what you did above using the while statement.  Yes, the MATLAB code is given below.

% Equivalent in while
% The loop below will calculate values of k^2-50
% for all values of the requested k for which k^2-50 is positive.
k=-10;
while (k<=10)
if (k^2-50>0)
val=k^2-50;
fprintf(‘\n k=%g  val=%g’,k,val)
end
k=k+1;
end

_______________________________________________________

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, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos

## The break statement in MATLAB

The break statement in MATLAB is used to break out of a loop – a for or while statement, that is, it terminates the execution of the loop.  Let’s suppose someone wants to find the value of k^2-50 for all integers in [-10,10] domain.  The mfile for that is given below.

% For integers k=-10,-9,….,9,10,
% the function k^2-50 will take positive as
% well as negative values.
%For example, for k=-9, k^2-50=31; for k=1,
% k^2-50=-49; for k=8, k^2-50=14.
% The loop below will calculate values of k^2-50 for all values of requested k.
for k=-10:1:10
val=k^2-50;
end

___________________________________________________________

Let’s suppose now you are asked to calculate value of k^2-50 for all integers in [-10,10] domain but only until k^2-50 becomes negative.

% The loop below will calculate values of k^2-50
% for all values of the requested k
% until it turns negative
for k=-10:1:10
if (k^2-50<0)
break;
end
val=k^2-50;
fprintf(‘\n k=%g  val=%g’,k,val)
end

____________________________________________________________

Can you do what you did above using the while statement.  Yes, the MATLAB code is given below.

% Equivalent in while
% The loop below will calculate values of k^2-50
% for all values of the requested k until it turns negative
k=-10;
while (k<=10) & (k^2-50>0)
val=k^2-50;
fprintf(‘\n k=%g  val=%g’,k,val)
k=k+1;
end

_______________________________________________________

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, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos

## How do I solve simultaneous linear equations given in equation form?

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

%% 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 set of simultaneous linear equations
% given in equation form?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/sle_equations.m;
% Last Revised : August 22, 2009;
% Abstract: This program shows you how to solve a set of
%     simultaneous linear equations given in equation form?
%           .
clc
clear all
clf

%% 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 K Kaw of https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/sle_equations.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   August 22, 2009’)
disp(‘ ‘)

%% INPUTS
% Enter the equations
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: 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

## How do I solve a set of simultaneous linear equations given in matrix form?

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

%% 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 set of simultaneous linear equations?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/sle.m;
% Last Revised : August 12, 2009;
% Abstract: This program shows you how to solve a set of simultaneous linear
% equations?
%           .
clc
clear all
clf

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to solve a’)
disp(‘   set of simultaneous linear equations’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://numericalmethods.eng.usf.edu/blog/sle.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   August 12, 2009’)
disp(‘ ‘)

%% INPUTS
% Enter the coefficient matrix of the equation [A][X]=[C]
A=[12  23  39; 13  17  19; 21  23  29];
% Enter the right hand side vector
C=[29;   37;  59];
%% DISPLAYING INPUTS
disp(‘  ‘)
disp(‘INPUTS’)
disp(‘________________________’)
disp(‘Coefficient Matrix’)
disp(‘________________________’)
dataval=[A];
disp(dataval)
disp(‘________________________’)
disp(‘Right hand side vector’)
disp(‘________________________’)
dataval=[C];
disp(dataval)
disp(‘________________________’)

%% THE CODE
% The solution
X=A\C;

%% DISPLAYING OUTPUTS
disp(‘  ‘)
disp(‘OUTPUTS’)
disp(‘________________________’)
disp(‘Solution Vector’)
disp(‘________________________’)
dataval=[X];
disp(dataval)
disp(‘________________________’)

his 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, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos

## How do I do spline interpolation 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 conduct spline interpolation.

• 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

%% 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 do spline interpolation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/interpolate_spline.m;
% Last Revised : June 20, 2009;
% Abstract: This program shows you how to do spline interpolation?
%           .
clc
clear all
clf

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to do spline interpolation?’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of https://autarkaw.wordpress.com&#8217;)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/interpolation_spline.m&#8217;)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   June 20, 2009’)
disp(‘ ‘)

%% INPUTS
% y vs x data to interpolate
% x data
x=[-1  -0.75  -0.5  -0.25   0.25  0.50 0.75  1];
% ydata
y=[-0.5  -0.5  -0.5  -0.5   0.5  0.5  0.5  0.5];
% Where do you want to interpolate at
xin=[-0.8  -0.7  0.7  0.8];
%% DISPLAYING INPUTS
disp(‘INPUTS’)
disp(‘The x data’)
x
disp(‘The y data’)
y
disp(‘The x values where you want to find the interpolated values’)
xin
disp(‘  ‘)

%% THE CODE
% Fitting to spline – it is cubic splines
yin=spline(x,y,xin);
% This is only for plotting the spline interpolants
% Find the number of data points
n=length(x);
xplot=x(1):(x(n)-x(1))/10000:x(n);
yplot=spline(x,y,xplot);

%% DISPLAYING OUTPUTS
disp(‘  ‘)
disp(‘OUTPUTS’)
disp(‘x values at which function is to be interpolated’)
xin
disp(‘y values at the xin values’)
yin
xlabel(‘x’);
ylabel(‘y’);
title(‘y vs x ‘);
plot(x,y,’o’,’MarkerSize’,10,’MarkerEdgeColor’,’b’,’MarkerFaceColor’,’b’)
hold on
plot(xin,yin,’o’,’MarkerSize’,10,’MarkerEdgeColor’,’r’,’MarkerFaceColor’,’r’)
hold on
plot(xplot,yplot,’LineWidth’,2)
legend(‘Points given’,’Points found’,’Spline Curve’,’Location’,’East’)
hold off
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

## How do I do polynomial interpolation 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 do polynomial interpolation.

• 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

%% 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 do polynomial interpolation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/interpolate_polynomial.m;
% Last Revised : June 10, 2009;
% Abstract: This program shows you how to do polynomial interpolation?
%           .
clc
clear all
clf

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to do polynomial interpolation?’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of https://autarkaw.wordpress.com&#8217;)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/interpolation_polynomial.m&#8217;)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   June 10, 2009’)
disp(‘ ‘)

%% INPUTS
% y vs x data to interpolate
% x data
x=[-1  -0.6  -0.2  0.2  0.6  1];
% ydata
y=[0.0385    0.1000    0.5000    0.5000    0.1000    0.0385];
% Where do you want to interpolate at
xin=[-0.8  -0.7  0.7  0.8];
%% DISPLAYING INPUTS
disp(‘INPUTS’)
disp(‘The x data’)
x
disp(‘The y data’)
y
disp(‘The x values where you want to find the interpolated values’)
xin
disp(‘  ‘)

%% THE CODE
% Find the number of data points
n=length(x);
% Fitting to polynomial of order m=n-1
m=n-1
% pp consists of the coefficients of the polynomial
% pp(1)*x^m+pp(2)*x^m+…….+pp(m)
% pp(1) is coefficient of x^m
% pp(2) is coefficient of x^(m-1)
% and so on
pp=polyfit(x,y,m);
% Getting the values at xin
yin=polyval(pp,xin);
% This is only for plotting the interpolating polynomial
xplot=x(1):(x(n)-x(1))/10000:x(n);
yplot=polyval(pp,xplot);

%% DISPLAYING OUTPUTS
disp(‘  ‘)
disp(‘OUTPUTS’)
disp(‘x values at which function is to be interpolated’)
xin
disp(‘y values at the xin values’)
yin
disp(‘These are the coefficients of the polynomial interpolant’)
disp(‘pp(1) is coefficient of x^m, pp(2) is coefficient of x^(m-1) and so on’)
fprintf(‘Order of polynomial m =%g’,m)
pp
xlabel(‘x’);
ylabel(‘y’);
title(‘y vs x ‘);
plot(x,y,’o’,’MarkerSize’,10,’MarkerEdgeColor’,’b’,’MarkerFaceColor’,’b’)
hold on
plot(xin,yin,’o’,’MarkerSize’,10,’MarkerEdgeColor’,’k’,’MarkerFaceColor’,’k’)
hold on
plot(xplot,yplot,’LineWidth’,2)
legend(‘Points given’,’Points found’,’Polynomial Curve’)
hold off
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