How do I solve an initial value ODE problem in MATLAB? Updated for MATLAB 2020a

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 an initial value ordinary differential equation

  • 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 an initial value ordinary differential equation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/ode_initial.m;
% Last Revised : May 14, 2009;
% Abstract: This program shows you how to solve an
% initial value ordinary differential equation.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘ This program shows you how to solve’)
disp(‘ an initial value ordinary differential equation’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘ Autar K Kaw of https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘ http://numericalmethods.eng.usf.edu/blog/ode_initial.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘ May 14, 2009’)
disp(‘ ‘)

%% INPUTS
% Solve the ordinary differential equation 3y”+5y’+7y=11exp(-x)
% Define x as a symbol
% Define y(x)n as a symbol also
syms x y(x)
%The ODE
Dy=diff(y);
ode_eqn=3*diff(y,x,2)+5*diff(y,x,1)+7*y == 11*exp(-13*x)’;
% The initial conditions
iv_1=Dy(0)==17;
iv_2=y(0)==19;
% The value at which y is sought at
xval=2.0;
%% DISPLAYING INPUTS

disp(‘INPUTS’)
func=[‘ The ODE to be solved is ‘ char(ode_eqn)];
disp(func)
iv_explain=[‘ The initial conditions are %s’ char(iv_1) ‘ ‘ char(iv_2)];
disp(iv_explain)
fprintf(‘ The value of y is sought at x=%g’,xval)
disp(‘ ‘)

%% THE CODE
conds=[iv_1 iv_2];
% Finding the solution of the ordinary differential equation
soln=dsolve(ode_eqn,conds);
soln=simplify(soln);
% vpa below uses variable-precision arithmetic (VPA) to compute each
% element of soln to 5 decimal digits of accuracy
soln=vpa(soln,5);

%% DISPLAYING OUTPUTS
disp(‘ ‘)
disp(‘OUTPUTS’)
output=[‘ The solution to the ODE is ‘ char(soln)];
disp(output)
value=subs(soln,x,xval);
fprintf(‘ The value of y at x=%g is %g’,xval,value)
disp(‘ ‘)

This post is brought to you by

Using Microsoft Forms as a Personal Response System

While the pandemic has shifted our face-to-face classes to remote learning, finding new tools that are simple to use for the student as well as the instructor is quite a task.

One of the tools I am using to create the initial discussion in my weekly sessions for my Numerical Methods course at the University of South Florida is the  Microsoft Forms Quiz.  The quiz replaces the personal response system I used to use as that now costs students more than $30 a semester.  Microsoft Forms Pro is free to use.  To get it, just go to forms.office.com.

The quiz question options include the multiple-choice, fill-in-the-blank, and multiple-answers type of questions.  I do not use the quiz for grading or attendance. Hence, I do not have to worry about LMS compatibility or academic integrity – the quiz is just a plain way for the student to gauge their understanding so far and for the instructor to know what they know well and on what they may need help.

To avoid repeating what others have written succinctly about how to make a quiz, go here to learn how to develop a quiz and add a question, and if you are a STEM educator and want to add equations or math symbols, see an example here.

Mobile Preview

The pallete of equations is limited but does serve my purpose mostly for a Numerical Methods course.

Equation Pallete

For example, the pallete does not have options for matrices and integrals, but you can get around that by using an image.  However, only one image can be added to a question stem.  On top of that, an image is not an available feature at all for the options in a multiple-choice question.  To get around the limitation of one image in the question stem and none in the options, you can save the whole question and the options as an image (in MS word, make your text with equations, cut and paste them as a picture, right-click to save the picture as an image; alternatively, take a screenshot and crop it), and then use the options within MS Forms to direct to the choices.  See the figure below for an example.  However, do make images of small size in width so that they fit well on a smartphone without having to magnify the image.  A width of fewer than 600 pixels and keeping the height to be of similar magnitude as the width works best.

Using an image to present a question with many equations

The choices for previews are for a computer and a mobile phone; themes are available, but it is best to use simple school colors; the quiz can be shared with anyone through a link or QR code or embedded link or email.

Share Options

Here is an example of a quiz I made for the course.  Try it out by clicking on the link or by using your smartphone camera app to scan the QR code below. No one is grading, and I will not know who you are.

QR Code

Options for responses include accepting responses or not, and the start and end time.

Settings

How do I use a quiz in the class?

I use a tablet for the synchronous online sessions. I use Blackboard Collaborate Ultra as the live-streaming app (video conferencing tool) to teach the class.  It would be similar if you are using Zoom, MS Teams, or YouTube live-streaming.

While streaming the class, I paste the QR code image in an OneNote file and also put the link to the quiz in the chatbox of the streaming app as an alternative.

I open the quiz on a separate computer (a dual monitor connected to your tablet will do as well) so that I can see the responses getting updated in real-time. This gauges when the discussion about the quiz should resume, and how it should be conducted.

How do I refer to the quiz during the discussion so that I can write on the quiz itself as well? I just make a PDF file of the quiz and insert it into an OneNote page. Creating the PDF file is tricky because only the first page of the MS Forms Quiz can be printed.  To get all pages of the quiz printed as a PDF file, open a browser and go to the shareable link you gave to the students.  Now you can print all the pages of the quiz.

While discussing the quiz, students communicate verbally via their microphone or the chatbox of the streaming app. You can step it up and use break-out rooms of your streaming app to practice using the think-pair-share technique.

___________________

This post is brought to you by

A short sample programming project to exhibit how to submit one

Here is a short sample of the submission expectations for a programming project in numerical methods.

The assigned sample project assignment [PDF]; the corresponding mfile [m]; the expected complete submission report [PDF]; learn how to make a single pdf file from various parts of the sample project [BLOG].

COVID19 Regression Model and Other Thoughts

March 28, 2020
I am not an epidemiologist but do regression modeling for a living. I have done regression modeling to predict student grades just after the first test, and now we will be using adaptive learning metrics to improve our identification schemes even earlier in the semester. It is not to spell boom or doom to a student but only to intervene early with personalized recommendations.
Looking at whatever data I can use and have time to scrape, four things are reasonably clear at this time to me about COVID19.
1) First, the rate of infection is exponential but it does not stay like that forever.  The logistic function of the infection rate is analogous to how the mass of a moving rocket decreases as it burns up its fuel. F=ma, but m is not a constant.
2) Second, President Trump is finally thinking about quarantining NY, NJ, CT area. A little late but it will definitely decrease the power of the exponent.
3) Third, we have to get more testing done but one which is totally random. We could have found the effect of the spring breakers coming to FL and of the college kids being sent home to parents who are having kids late in life. Please do not send your grandkids to grandpa/grandma’s retirement home. They may be the children of the corn.
4) Fourth, Florida and Louisiana need to get their head straightened out and use tougher rules to keep people inside and a method to keep outsiders out. They are the next hot zone.

On Making A Video Lecture on iPad and Uploading to YouTube

On Making A Video Lecture on iPad and Uploading to YouTube

Guest Blogger: Rasim Guldiken

Date: March 20, 2020

While we are scrambling to take the courses we teach from face-to-face format to remote format, we all could use simple tools to make the process simple and easy to implement.  I have created two extensive tutorials.

The first tutorial  shows step-by-step instructions on screencasting your lecture with an iPad using any stylus, editing the video in the Imovie app of iPad, uploading it to YouTube, or directly to your LMS.

The second tutorial illustrates on how to embed any YouTube video to your Canvas LMS course content.

I have been recording and uploading the videos to my Canvas course since Spring 2019, but following the same procedure has been frustrating as it takes several hours for videos to process. I really can’t blame anyone; there is an unprecedented load to the servers.

That is the reason I created this alternative method tutorial on using 1) YouTube as a platform to upload and store your content  and then 2) embedding the videos to Canvas.

This process works flawlessly as the videos are not stored on the LMS servers.

About the guest blogger

Dr. Rasim Guldiken is an Associate Professor of Mechanical Engineering at the University of South Florida.  His engineering education interests lie in open courseware for a course in Fluid Mechanics, metacognitive activities, and flipped learning.

Why Do We Use Numerical Methods?

Numerical methods are techniques to approximate mathematical procedures (an example of a mathematical procedure is an integral).

Approximations are needed because we either cannot solve the procedure analytically (an example is the standard normal cumulative distribution function)

or because the analytical method is intractable ( an example is solving a set of a thousand simultaneous linear equations for a thousand unknowns for finding forces in a truss)

_______________

This post is brought to you by

Why Don’t I Allow (Not Ban) Use of Cell Phones in Class – An Open Letter to Students?

Dear Students:

You are not allowed to use cell phones in this class — it is considered academic disruption. If you are expecting an important call or text, please let me know in advance and I will advise to sit at desks close to the door. You can quickly go outside to take the call or reply to your text. In a class of over a hundred, only a few people take me up on this offer, but the offer is available to you. “Loved ones though should call 911 in case of an emergency — the local responders will be there faster than you can.”

If you have an approved request for accommodation through the Student Disability Services to use your cell phone, please use it as per the allowed guidelines — just let me know in the first week of classes. You are being an exception to the policy is nobody’s business including mine. Do not be concerned about being “outed” — we all are outed every day – some are in a wheel chair while others use a skateboard, some can only take the elevator while others only take the stairs; some of us are not good looking while others are blessed with a head full of hair; some wear a head dress, while others have a skullcap on.

You are allowed to use flat laptops and tablets only if they are used to take notes with a stylus. A student cannot claim they want to type notes as that is virtually impossible to do in an engineering course lecture that is full of equations and sketches. Anyway, taking notes by hand is shown to be cognitively better than typing.

About those students who may mention personal responsibility and that disallowing the cell phone is an ego trip for me, how many studies do I need to show these students about negative effects of multitasking and working memory when one is learning something new.

The student use of cell phones distracts others and there are studies on that too.

Since we are using active learning in class, questions are encouraged even more so. It is harder to discern if a student is asking a question because of inattention or lack of understanding.  The former is a waste of precious class time for the rest of us.

And more importantly, removing temptation is much better than self-control. We only have so much self-control quota during the day – so having a no cell phone policy is helping the student to preserve some self-control for more important issues of the day.

Some may say, “Be more interesting than the incoming text”. I just cannot compete, as learning something new is hard and looking at a “Facebook like” is an immediate high.

Control the machine; do not let the machine control you.

Best of Learning
Autar Kaw
Your Instructor for Numerical Methods

______________________

This post is brought to you by

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’)
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

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

  • 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’)
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