How many significant digits are correct in my answer?

To find how many significant digits are correct in my answer in a numerical method that gives iterative values, one finds the absolute relative approximate  percentage error defined as
|(Current approximation-Previous approximation)/Current approximation|*100
If the absolute relative approximate percentage error is less than or equal to 0.5*10^(2-m), then m significant digits are at least correct in the answer.
For example, if you want

  • at least 1 signficant digit to be correct in your answer, your absolute relative approximate error should be less than or equal to 5%
  • at least 2 signficant digit to be correct in your answer, your absolute relative approximate error should be less than or equal to 0.5%
  • at least 3 signficant digit to be correct in your answer, your absolute relative approximate error should be less than or equal to 0.05%
    and so on.

In the next blog, we will illustrate this concept via an example.
___________________________________________
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.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Do we have to setup all 3n equations for the n quadratic splines for (n+1) data points?

QUESTION ASKED BY STUDENT: For linear splines, we still find each line individually. In quadratic, we are forced to find every equation at once, correct? Which is where the matrix comes from?

ANSWER: That sounds right, but one can find quadratic equations individually also .
How?
If the first spline is linear, then that spline can be found by the two points it goes through.
Then the second spline goes thru two points, and that gives two equations.  The slope of the second spline is same as the slope of first spline at the common interior point, and the first spline is known. This gives the third eqn.  This process can be repeated for each following spline.

____________________________________

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.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Checking if a number is non-negative or not?

Many students ask me how do I do this or that in MATLAB.  This is a new addition to the “How do I do that in MATLAB“  series.
 
In this blog, I show you how to use the if-else-end statement to write a simple program of finding if a number is non-negative or not.
 
Here are the links for the program.
The mfile is here
The published version of the mfile is here
 

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.
%% TOPIC
% How do I check if a number is non-negative or negative
%% SUMMARY
% Language : Matlab 2010a;
% Authors : Autar Kaw and Sri Harsha Garapati;
% Mfile available at
% http://nm.mathforcollege.com/blog/if_else_end_statements_blog.m
% Last Revised : January 23, 2012;
% Abstract: This program shows you how to check if a number is negative or
% non-negative using if-else-end statement in MATLAB
clc
clear all
%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to check if a number is negative or’)
disp(‘   non-negative using if-else-end statement in MATLAB’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/if_else_end_statements_blog.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   February 13, 2012’)
disp(‘ ‘)

%% INPUTS
 
% Enter the number to be tested
a=4;
%% DISPLAYING INPUTS
disp(‘  ‘)
disp(‘INPUTS’)

% Printing the input number
fprintf(‘The input number to be tested is %g\n’,a)
%% CODE AND DISPLAYING OUTPUT
disp(‘  ‘)
disp(‘OUTPUTS’)

% using the if-else-end statement to check
if (a>=0)
    disp(‘The number is nonnegative’)
else
    disp(‘The number is negative’)
end

_________________________________________________

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.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Largest integer that can be represented in a n-bit integer word

To find the largest integer in base-10 that can be represented in an n-bit integer word, let’s do this inductively.

  • If you have 3 bit-word, the highest number is (111) of base-2 which is 7 (1*2^2+1*2^1+1*2^0) of base-10,
  • If you have 4 bit-word, the highest number is (1111) of base-2 which is 15 (1*2^3+1*2^2+1*2^1+1*2^0) of base-10,
  • if you have 5 bit-word, the highest number is (11111) of base-2 which is 31 (1*2^4+1*2^3+1*2^2+1*2^1+1*2^0) of base-10. 

There is a trend here: 3 bit-word stores a maximum number of 7 (2^3-1), 4-bit word stores a maximum of 15 (2^4-1), 5 bit-word store a maximum number of 31 (2^5-1), and so on.   This means that the maximum number stored in n-bit word stores a maximum number of 2^n-1. 

We can derive the maximum number by knowing that the maximum base-10 number in a n-bit word is the summation series:
1*2^(n-1)+1*2^(n-2)+………+1×2^0. 
This is a geometric progression series.  The formula for the sum of a geometric series
        a+ar+ar^2+…+a*r^n =a*(1-r^(n+1))/(1-r), r ≠ 1,
Hence,
       1*2^(n-1)+1*2^(n-2)+………+1×2^0=1*(1-2^(n))/(1-2)=2^n-1 _____________________________________________________________

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.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Printer cuts off MATLAB code and text

When you print MATLAB code or the published form of the code, you may find the code or the comments getting cut off.  The solution to these problems is given at several places in MATLAB blogs and documentation.  In this blog, I have summarized what to do as the issues of code getting cut off falls into three categories
 

___________________________________________

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.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Differentiating a Discrete Function with Equidistant Points

Many students ask me how do I do this or that in MATLAB.  This is a new addition to the “How do I do that in MATLAB”  series.
 
In this blog, I show you how to find the first derivative of a discrete function y(x).  We are assuming that the x values are eqidistant and the data is sorted in ascending or descending order by the x values.  The latter requirement can be relaxed easily in programs such as MATLAB where one can use the sortrows command to put the data in the required order.
 
To keep the accuracy of all calculated first derivatives to be the same, we use the following formulas:
 
For the first data point, we use the forward divided difference formula
        f ‘(x) =(-f(x+2h) + 4 f(x+h) -3 f(x))/(2h)+order(h^2)
 
For the interior points, we use the central divided difference formula
       f ‘(x) =(f(x+h) -f(x-h))/(2h)+order(h^2)
 
For the last data point, we use the backward divided difference formula
       f ‘(x) =(f(x-2h) – 4 f(x-h) +3 f(x))/(2h)+order(h^2)
 
Here are the links for the program.
The mfile is here
The published version of the mfile is here

%% 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 find the first derivative of a discrete function y(x) if the
% x values are equidistant.
%% SUMMARY
% Language : Matlab 2010a;
% Authors : Autar Kaw and Sri Garapati;
% Mfile available at
% http://nm.mathforcollege.com/blog/discrete_diff_equidistant_blog.m
% Last Revised : January 17, 2012;
% Abstract: This program shows you how to differentiate discrete data if
% the x values are equally spaced
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to differentiate discrete data if’)
disp(‘   the x values are equally spaced ‘)

disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar Kaw and Sri Garapati of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/discrete_diff_equidistant_blog.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   January 17, 2012’)
disp(‘ ‘)

%% INPUTS

% Inputs assuming
%    that three or more points are given
%    all x values are equidistant
%    x values are in ascending or descending order.
x=[2.3   3.4   4.5    5.6   6.7   7.8];
y=[4.6   7.9   13.0   12.3  3.2   1.9];
%% DISPLAYING INPUTS
disp(‘  ‘)
disp(‘INPUTS’)
% Creating a matrix to print input data as a table
data=[x;y]’;
disp(‘   X Data     Y Data’)
% Printing the input data as a table
disp(data)

%% THE CODE

% n returns the number of data points
n=length(x);
% delta is the distance between consecutive x values
delta=x(2)-x(1);

% “dy” is an array which stores the value of the derivative at each x-value

% finding the derivative from the 2nd order accurate forward divided
% difference formula at the first data point
dy(1)=(-y(3)+4*y(2)-3*y(1))/(2*delta);

% finding the derivative from the 2nd order accurate central divided
% difference formula at the second to (n-1)th data point
for i=2:1:n-1
    dy(i)=(y(i+1)-y(i-1))/(2*delta);
end

% finding the derivative from the 2nd order accurate backward divided
% difference formula at the first data point
dy(n)=(y(n-2)-4*y(n-1)+3*y(n))/(2*delta);

% creating a matrix with input data and calculated derivative value at
% each data point for printing as a table
xdy=[x’ y’ dy’];

%% DISPLAYING OUTPUTS
disp(‘  ‘)
disp(‘OUTPUTS’)
disp(‘     XData   YData  Derivative’)
% printing the input data points and calculated derivative values(Outputs)
disp(xdy)
_________________________________________________

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.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Saylor Foundation Harnesses Numerical Methods Resources

 Saylor Foundation (http:/saylor.org) is harnessing quality open courseware resources available around the world.   More than 90% of the resources for the Numerical Methods for Engineers course at the site http://www.saylor.org/courses/me205/ is composed of content from the http://nm.mathforcollege.com website.

__________________________________

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.

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.

Audiovisual Lectures for Novice Programmers

The other day, a student asked me if I would recommend freely-available online digital audiovisual lectures  for those students who are learning programming for the first time.  I can point out three sources I have used myself and to say the least, I have been inspired.  Over the last two years, I have used their teaching techniques and examples in my own 1-credit hour EML3035 Programming Concepts course at the University of South Florida.  I do not have audiovisual lectures but I have several scripts to help you along if you use MATLAB.  Let me know what you think.

1. Harvard’s David Malan: Introduction to Computer Science   

2. MITs duo Eric Grimsom and John Guttag: Introduction to Computer Science and Programming

3. Stanford’s Mehran Sahami Computer Science I: Programming Methodology

____________________________________

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.