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 http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/interpolation_polynomial.m’)
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 and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

How do I solve a boundary value ODE 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 solve a boundary 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 a boundary value ordinary differential equation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/ode_boundaryvalue.m;
% Last Revised : May 26, 2009;
% Abstract: This program shows you how to solve an
%           boundary value ordinary differential equation.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to solve’)
disp(‘   a boundary value ordinary differential equation’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/ode_boundaryvalue.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   May 26, 2009’)
disp(‘ ‘)

%% INPUTS
% Solve the ordinary differential equation
% y”-0.0000002y=0.000000075*x*(75-x)
% Define x as a symbol
syms x
%The ODE
ode_eqn=’D2y-0.000002*y=0.000000075*x*(75-x)’;
% The initial conditions
iv_1=’y(0)=0′;
iv_2=’y(75)=0′;
% The value at which y is sought at
xval=37.5;
%% DISPLAYING INPUTS

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

%% THE CODE

% Finding the solution of the ordinary differential equation
soln=dsolve(ode_eqn,iv_1,iv_2,’x’);
% 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 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 and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

How do I solve a nonlinear equation that needs to be setup 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 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.

%% 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 nonlinear equation if I need to set it up?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/integration.m;
% Last Revised : March 28, 2009;
% 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(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/nonlinearequation.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   April 17, 2009’)
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
f=[num2str(C1) ‘-3*x^2*(‘ num2str(R) ‘-x/3)’]
% Finding the solution of the nonlinear equation
soln=solve(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: 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 and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

How do I integrate a continuous function 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 integrate a continuous function.

The MATLAB program link is here.

The HTML version of the MATLAB program 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 integrate a continuous function?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/integration.m;
% Last Revised : March 28, 2009;
% Abstract: This program shows you how to integrate a given function.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to integrate’)
disp(‘   a given function ‘)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/integration.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   March 29, 2009’)
disp(‘ ‘)

%% INPUTS

% Integrate exp(x)*sin(3*x) from x=2.0 to 8.7
% Define x as a symbol
syms x
% Assigning the function to be differentiated
y=exp(x)*sin(3*x);
% Assigning the lower limit
a=2.0;
% Assigning the upper limit
b=8.7;

%% DISPLAYING INPUTS

disp(‘INPUTS’)
func=[‘  The function is to be integrated is ‘ char(y)];
disp(func)
fprintf(‘  Lower limit of integration, a= %g’,a)
fprintf(‘\n  Upper limit of integration, b= %g’,b)
disp(‘  ‘)
disp(‘  ‘)

%% THE CODE

% Finding the integral using the int command
% Argument 1 is the function to be integrated
% Argument 2 is the variable with respect to which the
%    function is to be integrated – the dummy variable
% Argument 3 is the lower limit of integration
% Argument 4 is the upper imit of integration
intvalue=int(y,x,a,b);
intvalue=double(intvalue);

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
fprintf(‘  Value of integral is = %g’,intvalue)
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 and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Numerical Methods YouTube Video Progress

Since January 2009, I have been videotaping numerical methods course lectures in the Educational Outreach studio of University of South Florida, Tampa. Videos are made in 10-minute segments, not just because that is the limit of the length of YouTube videos, but because we firmly believe in making our resources pedagogically neutral. Ten minute videos allow an instructor to pick and choose what, when and how he or she wants the students to learn.

But some people have asked me – Why YouTube – why not put the same 10-minute videos on your own website. We do have the links to the YouTube videos on our own website, but there are many good reasons to go the YouTube route. The list of reasons below may be obvious to some, while others may not have thought about some of them.

1. Use YouTube’s storage space for the videos.

2. Use YouTube’s compression technology to make the videos stream faster on slow connections.

3. Use the power of Google and YouTube to tag and search the videos.

4. Use YouTube’s bandwidth as opposed to that of my school. My school’s IT department most probably would start screaming when the downloads pick up pace.

5. Use YouTube’s editing facilities to add annotations, links, and play lists.

6. Use the ubiquity of YouTube to reach a large audience.

7. Simple one stop process to let others embed the videos on their website.

8. Have open discussion on the videos via comments.

9. Get the videos rated so that we can judge their quality.

10. Use the “insight” tool of YouTube to analyze who is watching the videos.

______________________________________________________________________

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 and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

An efficient formula for an automatic integrator based on trapezoidal rule

In the previous post, we discussed why doubling the number of segments in the automatic integrator based on multiple-segment trapezoidal rule is more efficient than increasing the number of segments one at a time. But this advantage involves having to store the individual function values from previous calculations and then having to retrieve them properly. This drawback can be circumvented very efficiently as explained below. What you will see is that there is no need to store individual function values.

Automatic Integrator Formula
Automatic Integrator Formula

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://www.youtube.com/numericalmethodsguy.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Why keep doubling the segments for an automatic integrator based on Trapezoidal rule?

Automatic Integrator
Automatic Integrator
Automatic Integrator
Automatic Integrator

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://www.youtube.com/numericalmethodsguy.  

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

A problem using central divided difference error order

This is a problem I asked in the first examination of my Numerical Methods course in Spring 2009.   The question is that if one gives you an approximate value of the derivative of a function at a certain point using the central divided difference formula for two different step sizes, would you be able to find a better estimate of the derivative?

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://www.youtube.com/numericalmethodsguy.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Audiovisual Lectures on Numerical Methods

We have started to put audiovisual lectures on the web for an undergraduate course in numerical methods. These videos are available at the youtube site http://www.youtube.com/numericalmethodsguy.

Some of the videos are longer than 10 minutes, so they could not be loaded to the youtube site. Right now, these videos and the links to the youtube videos are available at http://nm.mathforcollege.com/videos/index.html. We are editing the longer than 10-minute videos so that they can be uploaded to youtube.

The above webpage is a better webpage to find the videos of your choice as the videos are given in a sequential fashion for a particular topic.

By end of Spring 2009, we should have uploaded all the audiovisual lectures for a typical numerical methods course.

Drop me a comment on what you think about this new avenue to learn numerical methods.

_____________________________________________________________

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://www.youtube.com/numericalmethodsguy.  

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Proper modeling needs to precede numerical solutions

Given below is a problem where it is shown that proper modeling is key to solving engineering problems.  An improper model and a proper model may lead to opposite conclusions


_________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com and the textbook on Numerical Methods with Applications available from the lulu storefront.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.