Integrating functions given at discrete points via MATLAB

When integrating functions given at discrete data points in MATLAB, trapz is the function of choice to go for. But we can get more accurate results by interpolating via cubic splines with the MATLAB spline function.  Since the spline function is made of piecewise cubics, Simpson’s 1/3rd rule can be used to exactly integrate them.   Here is a test program and a function written for you.  You can download the mfile here and a published version here for more readability if you wish.

clc
clear all
% Author: Autar Kaw, AutarKaw.com
% https://creativecommons.org/licenses/by-nc-sa/4.0/ 
% Testing the program with data given at discrete points
% for y=x^6
xx=[1  1.5    2   2.5     3     3.5    4   4.5   5];
yy=[1  1.5^6  2^6 2.5^6   3^6   3.5^6  4^6 4.5^6 5^6];
n=length(xx);
splineintegval=splineintegral(xx,yy);
fprintf('Value of integral using spline =%g',splineintegval)
% Exact value of integral if function was given continuously 
syms x
exact=vpaintegral(x^6,x,xx(1),xx(n));
fprintf('\n Value of integral using exact integration =%g',exact)
%% Function to integrate via spline interpolation
function splineval=splineintegral(x,y)
% This function integrates functions given at discrete data points
% INPUTS
% The x-values are given in ascending order 
% The limits of integration are x(1) to x(n), where n is teh length of
% the x-vector.
% OUTPUTS
% Integral of y dx from x(1) to x(n)
% Author: Autar Kaw, AutarKaw.com 
% https://creativecommons.org/licenses/by-nc-sa/4.0/ 
% The function finds the mid-point value of y between the given 
% x-values at the mid-point. Then since the spline is made of cubics,
% it uses the Simpson's 1/3rd rule to integrate the cubics exactly
n=length(x);
m=n-1;
% Calculating mid-points
    for i=1:1:m
        xmid(i)=(x(i)+x(i+1))*0.5;
    end
% Calculating value of y at the midpoints
polyvalmid=spline(x,y,xmid);
% Using Simpson's 1/3rd rule of integration to integrate cubics
splineval=0;
    for i=1:1:m
        splineval=splineval+(y(i)+y(i+1)+4*polyvalmid(i))*(x(i+1)-x(i))/6;
    end
end

____________________________

This post is brought to you by

Quick Start Guide to OpenMCR Program for a Single Key Exam

Ian Sanders and Autar Kaw


Step 1: Print out the required MCQ sheets
      • 75 Question Variant (Download from here or here)
      • 150 Question Variant (Download from here or here)

Step 2: Conduct the test

Give your printed question paper and the blank MCQ sheet to each of the students.  Ask them to bubble items such as last name, first name, middle name, student id, course number, key code (skip this as it is not necessary), and their answer responses.

A detailed explanation is given here


Step 3: Make the key

To create an answer key, simply print a normal sheet and put 9999999999 in the Student ID field. Fill in the exam with the correct answers.

A detailed explanation is given here

Step 4: Download the program to your PC

Go here https://github.com/iansan5653/open-mcr/releases and download the open-mcr.zip file that is available under assets.  Extract the zip file to the directory of your choice.   One of the files you will see amongst the extracted files is open-mcr.exe. That is the one you need to double-click on to run the program.

A detailed explanation is given here

Step 5:

One of the files you will see amongst the extracted files from Step 2 is open-mcr.exe. That is the one you need to double-click on to run the program.

Select the input folder where the images of the MCQ sheets are.

Choose the proper Form Variant as 75 Questions or 150 questions.

Under Select Output Folder, click Browse, and select the folder where you would like to save the resulting CSV files.

Press Continue and watch the progress of the program.

          A detailed explanation is given here


Step 6: Output Files

After the program finishes processing, results will be saved as CSV files in your selected output folder. To get the scores, open the scores csv file with Excel or a text editor.\

          A detailed explanation is given here


A multiple-choice question response reader

Ian Sanders and Autar Kaw

As we are back in face-to-face classes, we may wish to again conduct multiple-choice question examinations in the classroom without the use of computers to alleviate academic integrity concerns, not have to depend on the reliability of the laptop battery and available WiFi, and not have to continue to make different tests every semester.

Background

Commercially available OMR (optical mark recognition) exam sheets, scanners, and processing software can cost educators and educational institutions thousands of dollars per year. In response to this, OpenMCR has been developed as a free and easy-to-use alternative. The tool includes a multiple-choice exam sheet and works with any scanner and printer.

Here are the steps to use the program.


Step 1: Print out the MCQ sheets

Depending on the number of questions being asked, whether it is less than 75 or more (the maximum number of questions that can be asked is 150), you will first choose one of the two pdf files to print.

        • 75 Question Variant (Download the pdf file from here)
        • 150 Question Variant (Download the pdf file from here)

Step 2: Download the program to your PC

Go here https://github.com/iansan5653/open-mcr/releases and download the open-mcr.zip file that is available under assets.  Extract the zip file to the directory of your choice.   One of the files you will see amongst the extracted files is open-mcr.exe. That is the one you need to double-click on to run the program.

For more details about this open resource software, go to https://github.com/iansan5653/open-mcr


Step 3: Choose your variant of the key

In addition to reading scanned images, the software can also automatically score exam results. It does this by comparing the provided keys with the output. There are three options for this, depending on which way you generate your exams:

1. One Exam Variant

This is the most common exam variant.  If you give every exam-taker the exact same exam, you can instruct them to leave the Test Form Code field blank on their sheets. In addition, leave that field blank on the answer key sheet. All exam results will be compared to the single answer key sheet provided. Skip to Step 4 if this is what you chose – no need to create confusion.

2. Shuffled Exam Variants

If you provide the exam-takers with multiple variants of the same exam, and these variants differ only in question order (in other words, each variant is simply shuffled), then you can score all of these with the same key file by providing a file that defines the orders of the shuffled variants.

Each row in this file represents a key, and each column represents the position that that question should be moved to.

For example, if the exam form A has questions 1, 2, and 3, the exam form B might have them in 3, 1, 2 order and C might have them in 3, 2, 1 order. This would result in the following arrangement file:

Test Form Code, Q1, Q2, Q3
             A,  1,  2,  3
             B,  3,  1,  2
             C,  3,  2,  1

If this were the file you upload, then all of the exams with form A would be left untouched while B and C would be rearranged to 1, 2, 3 order. Select the file in the program under the Select Form Arrangement Map.

Note that the first row in this file should always be in 1, 2, 3, … order, and each row after that should only have one instance of each number.

If you use this option, only one exam key can be provided or an error will be raised.

3. Distinct Exam Variants

Finally, you can provide the exam-takers with multiple wholly distinct variants of the same exam. In this case, each exam will be scored by selecting the answer key with an exactly matching Test Form Code. No rearrangement will be performed.


Step 4: Conduct the test

Give the question paper and the blank MCQ sheet to the students.  Ask them to bubble items such as last name, first name, middle name, student id, course number, key code, and their answer responses.


Step 5: Scan the MCQ sheets and the key(s)

If you would like to take advantage of the automatic grading feature of the software, you must provide it with one or more answer keys. To create an answer key, simply print a normal sheet and put 9999999999 in the Student ID field. Also, add a Test Form Code which will be used to match students’ exams with the correct answer key, and finally, fill in the exam with the correct answers.

This is optional – you can choose to just have the software read the exams and not score them.

Scan all the student and key MCQ sheets on a copier or a scanner into a single PDF file.  Use Adobe Acrobat DC or any other freely available program to export the pdf file to images.


Step 6: Run the program

One of the files you will see amongst the extracted files from Step 2 is open-mcr.exe. That is the one you need to double-click on to run the program.

Select the input folder where the images of the MCQ sheets are.

If you select the convert multiple answers in a question to ‘F’ option, then if a student selects, for example, A and B for a question, the output file will save that as F instead of [A|B].

If you select the save empty in questions as ‘G’ option, if a student skips a question by leaving it blank, the output file will save that as G instead of as a blank cell.

Choose the proper Form Variant as 75 Questions or 150 questions.

Under Select Output Folder, click Browse, and select the folder where you would like to save the resulting CSV files.  If you select the sort results by name option, results will be sorted by the students’ last, first, and middle names (in that order). Otherwise, results will be saved in the order they were processed.

Press Continue and watch the progress of the program.


Step 7: Output Files

After the program finishes processing, results will be saved as CSV files in your selected output folder. These files can be opened in Excel or in any text editor. Files will be saved with the time of processing to avoid overwriting any existing files.

If you did not include any answer keys, one raw file will be saved with all of the students’ selected answers, and no scoring is performed.

If you did include one or more answer keys, two more files will be saved in addition to the aforementioned raw file. One of these files will have all of the keys that were found, and the other will have the scored results. In the scored file, questions are saved for each student as either 1 (correct) or 0 (incorrect).


Software License

Copyright (C) 2019-22 Ian Sanders

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

For the full license text, see license.txt.

Multiple-Choice Sheet License

The multiple-choice sheet distributed with this software is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license (CC BY-NC-SA 4.0). In summary, this means that you are free to distribute and modify the document so long as you share it under the same license, provide attribution, and do not use it for commercial purposes. For the full license, see the Creative Commons website.

Note: You are explicitly allowed to distribute the multiple-choice sheet without attribution if using it unmodified for educational purposes and not in any way implying that it is your own work. This is an exception to the Creative Commons terms.

Getting last name and first name from full name with a delimited comma

Rather than using the “text to columns” procedure in excel, if you want to use a function to separate the last name and the first name for simplicity of use and the ability to copy the functionality anywhere, here are two functions that are written for you.

I wrote these functions because CANVAS LMS does not have separate columns for first and last names in the grade book.   However, they do have an option of showing the full name delimited by a comma.  This is preserved when you export the GradeBook.

Figure 1: Display name as separated by commas.

To use them, press Alt-F11.  It will open up the Microsoft Visual Basic for Applications window.  Choose Insert>Module.  It will show up as Module1 by default in the VBA Project window.  Good to rename the module to say “LastFirstNameBreak” using the Properties Window.  Cut and paste the two functions in the module, and save your excel file.  You will need to save the excel file as an .xslm file though.

Figure 2. Microsoft VBA Window.  Functions are shown below.

Function BreakLastName(FullName)
' This function separates the last name from the
' full name that is delimited by a comma
FullNameTrim = Trim(FullName)
leng = Len(FullNameTrim)
' Loop checks where the comma is
For i = 1 To leng
If Mid(FullNameTrim, i, 1) = "," Then
ival = i
Exit For
End If
Next i
BreakLastName = Left(FullNameTrim, ival - 1)
End Function
Function BreakFirstName(FullName)
' This function separates the first name from the
' full name that is delimited by a comma
FullNameTrim = Trim(FullName)
leng = Len(FullNameTrim)
For i = 1 To leng
If Mid(FullNameTrim, i, 1) = "," Then
ival = i
Exit For
End If
Next i
BreakFirstName = Right(FullNameTrim, leng - ival - 1)
End Function

To use the functions, just use them like any other Excel function.  BreakLastName separates the last name, while BreakFirstName separates the first name.

Figure 3. Using the functions in an Excel spreadsheet.

____________________________

This post is brought to you by

How to Make a PDF file

Before COVID19 hit our lives, it was so easy for the student to submit a hard copy.  Now with soft copies being asked for, they and the instructors have to learn a bit.

So, I had this programming assignment before COVID19 closed our face-to-face classes. It involved handwritten pages, a published file, and writing out a short description of conclusions with equations in a word processor.  The student would print them all out at home or at the university, and collate them.  But now it has to be scanned and uploaded to the CANVAS learning management system.

So, if these three types of documents were to be uploaded as one pdf file, what would you do.  I am assuming that everyone has a smartphone (an iPhone or an android phone)

If you have a printer and a scanner

Print all the documents and scan the document as a pdf file

If you have a printer but no scanner

Print all the documents.  Download the CamScanner app on your iPhone or Android and follow the instructions of this YouTube video.

https://www.youtube.com/watch?v=UkYlGa3y4tk?start=0&end=196

Follow the directions up to the time it tells you how to email the document as I do not want you to email it to me.

If you have neither a printer nor a scanner

a) Take the handwritten documents. Download the CamScanner app on your iPhone or Android and follow the instructions of this

https://www.youtube.com/watch?v=UkYlGa3y4tk?start=0&end=196

Follow the directions up to the time it tells you how to email the document as I want you to upload it to CANVAS.

b) For typing the pages that include equations, use MS 365 Word, and develop your document.  Save it as a .docx file first, and only then save it as .pdf file.  You can save a document as a pdf file within MS 365 Word itself.

c) a MATLAB file can be published as a pdf file. See https://blog.autarkaw.com/2020/03/25/how-to-publish-in-matlab/

d) Merge pdf documents with Acrobat Pro. Did you know as a USF student you can download Adobe Acrobat for free?  It is a tool to make PDF files from wordprocessor docs as well as to merge them with other PDFs.  If you are a reader outside USF and do not have Acrobat Pro as it costs money, use https://smallpdf.com/merge-pdf. Just drag the pdf files, and it will merge them for you.

____________________

The brute force way

(not recommended though but desperate times require desperate measures)

a) Take photos of each handwritten page.  Send the images to your email and save them on your computer.   Insert images (Insert -> Pictures in Word Menu) of the handwritten work into an MS 365 Word doc. But check your file size. It can get too big for CANVAS to handle and your internet to upload.  The limit may be 100MB.

b) For typing the pages that include equations, continue with the MS Word doc.

c) a MATLAB file can be published as a doc file. See https://blog.autarkaw.com/2020/03/25/how-to-publish-in-matlab/  Cut and paste it into the word doc.

d) Save it as a .docx file first, and only then save it as .pdf file.  You can save a document as a pdf file within MS 365 Word itself.

______

This post is brought to you by

Stripping the tags from an HTML file

Recently, I was moving my webpages created through FrontPage 2003 (please stop snickering -it was a great tool for sites with flat web files) to WordPress.

This required clearing of formatting of the HTML file and one could use http://StripHTML.com for doing so – “StripHTML.com gives you a quick, easy and satisfying way to transform your ugly formatted and/or HTMLified text into a clean and pretty text for you to enjoy.”  However, most of us want to preserve the URL links, titles and the paragraph tags.

This VBA program that can be used within Excel takes an input text file, reads it as a single string, removes all tags except the URLs, paragraphs and titles, and writes to a new text file.

The VBA program below has the input file at “C:\Users\Tranq\Documents\abc\inputhtml.txt”, and outputs to “C:\Users\Tranq\Documents\abc\outputhtml.txt” and you can change these locations in the TextFile_PullData and TextFile_Create functions, respectively.  You can modify the program to read several files in a directory and make it even more efficient.

How to Use: Just open an Excel file. Press Alt+F11, and open a new module. Cut and paste what is given below, or download the text file.  Go to a cell in the excel file and enter =CleanTags(A1) in cell B1.  Make sure you have the input file at C:\Users\Tranq\Documents\abc\inputhtml.txt”.   The location can be modified.

**********************************************

Function CleanTags(HTML As String) As String
‘PURPOSE: Clean HTML tags except the paragraph, weblinks and title
‘ You can comment out if you want these to supress these as well
‘ The instructions are given at the spots.
‘ SOURCE: Unknown and AutarKaw.org
Dim result As String, StripIt As Boolean, c As String, i As Long
HTML = TextFile_PullData()

‘StripIt is used to figure out to include or exclude in output
StripIt = False

‘Looking at each character in the HTML file
For i = 1 To Len(HTML)
‘c is each character
c = Mid(HTML, i, 1)
‘Some conditions to take care for end of input file
If i <= Len(HTML) – 1 Then
‘d is last two characters of file to capture <a and <p
‘Just in case
d = Mid(HTML, i, 2)
Else
d = “”
End If
If i <= Len(HTML) – 3 Then
‘e is last four characters of file to capture </a> and </p>
e = Mid(HTML, i, 4)
Else
e = “”
End If

‘Checking for < character that begins an HTML tag
If c = “<” Then StripIt = True

‘Comment this if you want to strip paragraphs
If d = “<p” Then StripIt = False
If e = “</p>” Then StripIt = False

‘Comment this if you want to strip URL tags and title tags as well.
If d = “<a” Then StripIt = False
If e = “</a>” Then StripIt = False

‘Adds to output or skips it
If StripIt = False Then result = result & c
‘Taking care of closing tag to change the StripIt Boolean
If c = “>” Then StripIt = False
Next i
CleanTags = result

‘Putting the output in a new file
abc = TextFile_Create(result)
‘Run the program by entering =CleanTags(A1) in a blank excel file
‘where you have this module. Puts Done in cell if it runs correctly
CleanTags = “Done”
‘This lets you know the work is done. Comment if you like.
‘MsgBox (“Done”)
End Function

Function TextFile_PullData()
‘PURPOSE: Send All Data From Text File To A String Variable
‘SOURCE: www.TheSpreadsheetGuru.com

Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String

‘File Path of Text File
FilePath = “C:\Users\Tranq\Documents\abc\inputhtml.txt”

‘Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

‘Open the text file
Open FilePath For Input As TextFile

‘Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)

‘Report Out Text File Contents
‘MsgBox FileContent

‘Close Text File
Close TextFile
TextFile_PullData = FileContent

End Function

Function TextFile_Create(HTML As String)
‘PURPOSE: Create A New Text File
‘SOURCE: www.TheSpreadsheetGuru.com

Dim TextFile As Integer
Dim FilePath As String

‘What is the file path and name for the new text file?
FilePath = “C:\Users\Tranq\Documents\abc\outputhtml.txt”

‘Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

‘Open the text file
Open FilePath For Output As TextFile

‘Write some lines of text
Print #TextFile, HTML

‘Save & Close Text File
Close TextFile

End Function


This post is brought to you by

An FE Exam Math Problem in Complex Algebra

“The Fundamentals of Engineering (FE) exam is generally the first step in the process of becoming a professional licensed engineer (P.E.). It is designed for recent graduates and students who are close to finishing an undergraduate engineering degree from an EAC/ABET-accredited program” – FE Exam NCEES

For most engineering majors, mathematics is a required part of the examination. Here is a question on complex algebra.

complex number fe exam

 

This post is brought to you by

MOOC on Introduction to Matrix Algebra released

Introduction to Matrix Algebra is available as a MOOC now on Udemy. Of course, it is free! https://www.udemy.com/matrixalgebra/.  You will have a lifetime access to 177 lectures, 14+ hours of high quality content, 10 textbook chapters complete with multiple choice questions and their complete solutions.

Learning Objectives are

  • know vectors and their linear combinations and dot products
  • know why we need matrix algebra and differentiate between various special matrices
  • carry unary operations on matrices
  • carry binary operations on matrices
  • differentiate between inconsistent and consistent system of linear equations via finding rank of matrices
  • differentiate between system of equations that have unique and infinite solutions
  • use Gaussian elimination methods to find solution to a system of equations
  • use LU decomposition to find solution to system of equations and know when to choose the method over Gaussain elimination
  • use Gauss-Seidel method to solve a system of equations iteratively
  • find quantitatively how adequate your solution is through the concept of condition numbers
  • find eigenvectors and eigenvalues of a square matrix

_____________________________________________________

This post is brought to you by

Friday October 31, 2014, 11:59PM EDT, November 1, 2014 3:59AM GMT – Release Date for an Opencourseware in Introduction to Matrix Algebra

In a true Netflix style, on Halloween night, Friday October 31, 2014 at 11:59PM EST, we are releasing all resources simultaneously for an open courseware on Introduction to Matrix Algebra athttp://mathforcollege.com/ma/.  The courseware will include

  • 150 YouTube video lectures of total length of approximately 14 hours,
  • 10 textbook chapters,
  • 10 online multiple-choice quizzes with complete solutions,
  • 10 problem sets, and
  • PowerPoint presentations.

So set your calendar for October 31 for some matrix algebra binging rather than candy binging.  For more info and questions, contact Autar Kaw.

Chapter 1: Introduction 

Chapter 2: Vectors

Chapter 3: Binary Matrix Operations

Chapter 4: Unary Matrix Operations

Chapter 5: System of Equations

Chapter 6: Gaussian Elimination Method  

Chapter 7: LU Decomposition

Chapter 8: Gauss-Seidel Method

Chapter 9: Adequacy of Solutions

Chapter 10: Eigenvalues and Eigenvectors

________________________
This post is brought to you by

 

codecademy.com looks promising

codecademy.com is offering promising free online courses to non-CS programmers.  It is an interactive way of learning how to program and it also assesses your learning gains via badges.  Go and give it a try.  You can get a quick glimpse of what they are trying to do by watching a CNN news clip.

__________________________________

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 athttp://nm.mathforcollege.com/videos.  Subscribe to the blog via areader or email to stay updated with this blog. Let the information follow you.