I’m thinking about teaching calculus using Matlab. This is far from being a new idea – people have been using Matlab in their teaching for as long as Matlab has existed – but it must be admitted that as far as calculus is concerned, Matlab is probably less well equipped than a computer algebra system such as Maple, Mathematica, or the open source Maxima and Sage. This is of course because Matlab is primarily a numeric and computational system, whereas Maple, Mathematica and the others are symbolic systems. And calculus, being primarily analytical and symbolic, is better served by them.
That being said, I think that in fact Matlab – used only for computation and graphics – could be used to great effect in a calculus course. Note that the new versions of Matlab contain the Symbolic Math Toolbox, which provides a Matlab interface to the CAS MuPAD. However, I’m rather against using MuPAD-in-Matlab. For one thing, it seems to be a fairly clumsy interface. And you have to declare variables as being symbolic, so as not to confuse Matlab with its own (numeric) variables. I think if you want to use a symbolic system, then do just that. If you want a numeric system, do just that.
My feeling is that using plain old unadorned Matlab would be a great help to engineering students, for whom the bulk of their computing is Matlab based. I think introducing symbolic computation would only add to their cognitive load, without necessarily helping their mathematical understanding. So my thoughts are to introduce the standard calculus material – limits, derivatives, integrals and their applications – analytically, and then use the numeric and graphical capabilities of Matlab to explore and enhance that material. And in fact there’s a lot you can do.
Here’s a small sample.
Limits
Limits can easily be explored numerically in Matlab. For example, the old warhorse
You could simply, for example, enter:
>> x = 0.1 >> sin(x)/x
and then replace x with 0.01, 0.001, and so on, and see what happens to the values of the function. You could first set
>> format long
to give you more decimal places.
Rather than entering a new x value each time, you could do it all in one go:
>> x = [0.1; 0.01; 0.001; 0.0001; 0.00001] >> sin(x)./x
but this presupposes that students understand the “dot” notation of Matlab. Maybe you could define the function first:
f = @(x) sin(x)/x
and then apply it to your vector x:
arrayfun(f,x)
And of course if you wanted you could create the vector x using
x = 0.1.^[1:5]'
but this last assumes a certain knowledge of Matlab’s workings. I would certainly teach function definition (such as above), and the command arrayfun.
Newton’s method
Who does not love and teach
?
Matlab is at a disadvantage here – without the symbolic toolbox, it can’t compute symbolic derivatives. However, it can perform differentiation of polynomials (treated as vectors of their coefficients). So we start here. For example, let’s solve
using Newton’s method, with a starting value of First, we define the function and its derivative:
>> p = [1 0 0 1 0 -1] >> f = @(x) polyval(p,x) >> df = @(x) polyval(polyder(p),x)
Putting this together into a Newton’s rule function:
>> nr = @(x) x-f(x)/df(x)
Now we can start using it. First the easy way:
>> 0.8 >> nr(ans) 0.808859649122807 >> nr(ans) 0.808730628358884 >> nr(ans) 0.808730600479393
for as long as we like.
Next, a slightly more sophisticated way:
>> a = [0.8] >> for i=1:6 a=[a(:);nr(a(i))];end >> a 0.80000000000000 0.80885964912281 0.80873062835888 0.80873060047939 0.80873060047939 0.80873060047939 0.80873060047939
For Newton’s method applied to non polynomial functions, you could enter the function and its derivative yourself:
>> f = @(x) exp(x)-x^2 >> df = @(x) exp(x)-2*x
and then proceed as above.
Drawing tangent lines
Again, first enter the function and its derivative:
>> f = @(x) exp(-x^2) >> df = @(x) -2*x*exp(-x^2)
Given an value, we can determine the values of the function and derivative at that point, and construct the tangent line:
>> a = 1.2 >> fa = f(a) >> dfa = df(a) >> t = @(x) dfa*(x-a)+fa
and we can sketch both together:
>> ezplot(f, [0,2]) >> hold on >> ezplot(t,[0,2])
Simpson’s rule
There are, at a conservative estimate, about 42,897 versions of Simpson’s rule in Matlab. I want to keep it as simple as possible, and not use any clever Matlab tricks. So here’s one way to calculate
.
First set up the function, the limits of integration, and the number of times Simpson’s rule will be used:
>> a = 0 >> b = 1 >> f = @(x) exp(-x^2) >> n = 4
Now we set up the nodes (where the integral will be evaluated):
>> x = linspace(a,b,2*n+1) >> h = (b-a)/(2*n)
and the weights – we can either just enter them by hand:
>> w = [1 4 2 4 2 4 2 4 2 4 1]
or
>> w = zeros(1,2*n+1) >> for i=1:n w(2*i-1:2*i+1)=w(2*i-1:2*i+1)+[1 4 1];end
Now put it all together:
>> s = sum(w.*arrayfun(f,x))*h/3
0.74682612052747
The last could of course be done as a single matrix product:
>> s = w*arrayfun(f,x))'*h/3
but I think the first method is conceptually easier.
Filed under: Uncategorized
Two minor points of disagreement with your first paragraph:
(1) I wouldn’t say that MATLAB is “less well equipped” for calculus, but rather “differently equipped”. (I cringe at the political correctness there, but anyway.) MATLAB does numerical and graphical stuff extremely well, better IMO than Maple. Maple does symbolic stuff extremely well, better quite obviously than straight MATLAB. Which leads me to the second point:
(2) I wouldn’t say, given the development of mathematics over the last 10-15 years, that calculus anymore is “primarily analytical and symbolic”. To have a modern understanding of calculus, I really do think that we have to put the numerical and graphical approaches to calculus on equal, if not greater, footing with the symbolic.
But that’s good news for using MATLAB in calculus. Because so many actual applications of calculus tend more toward the graphical and numerical, so much the better for those wanting to use MATLAB, whose strengths lie in exactly those approaches.
Also, although we have Maple on our network at my college, I think a combination of MATLAB for numerical and graphical problems and Wolfram|Alpha for symbolic calculations is a powerful combination. With W|A the expense of doing automatic symbolic calculations just went down several orders of magnitude.
Finally, it’s worth noting that although (as you point out) the Symbolic Toolbox is an absurd way to do calculations from the MATLAB command line, using the MuPad notebook interface is actually pretty natural. It has a nice look-and-feel similar to Maple (although not as nice as Maple), gives you pretty-print output, and all that. All the symbolic calculations you showed above from the command line would be done in MuPad with much less fuss.
So the prognosis for using MATLAB in calculus is even better than you indicated! That’s good news, right? :)
Thanks for that – in fact I had those concerns myself even as I was writing. And I do agree that modern calculus is – or at least should be – as much computational and graphical as symbolic.
I used to use MuPAD a lot in the (good old) days when it was available for free, and came to be very impressed with it. And I don’t know if my university’s Matlab license includes the symbolic toolbox – my own private Matlab licence certainly doesn’t!
Given my druthers I don’t think I’d choose Matlab for teaching calculus, but for my engineering students, who use Matlab extensively through their courses, it’s probably the best bet.
FWIW, I am teaching a new course at my college next semester on MATLAB, and the primary audience contains almost no engineers — mainly “straight” math majors and education majors, with a few chemistry and biology majors. We’ll see how accessible MATLAB really is!