# Numerical methods on piecewise-continuous functions4

Posted by Jiro Doke,

Jiro's pick this week is Chebfun v2 by the Chebfun Team.

### Thorough Documentation

I was initially drawn to this entry because of the comprehensive documentation that was included with it. You can directly view it from the File Exchange page:

Their user's guide is extremely detailed and helpful. I had very little prior knowledge of the concepts covered by this tool, but after reading through it, I've come to appreciate the usefulness and the power of the tool. They created the guide using MATLAB's publishing functionality, which is an extremely effective way of providing documentation and examples for the end-users, because of the ability to intermix code, figures, explanatory text, and equations. Find more information about publish from this page on our Academic website.

### Chebfun Function

Now, let's take a look at the tool.

Borrowing the words from their documentation, a chebfun is a function of one variable defined within a particular interval. It allows you to perform symbolic-like operations with the performance of numerics. The underlying implementation of chebfun is based on the mathematical fact that smooth functions can be represented very efficiently by expansions in Chebyshev polynomials, similar to how Fourier series work with smooth periodic functions. By decomposing a function into these polynomial interpolants, many mathematical operations can be carried out efficiently.

Let's define a chebfun for within the interval [0 2].

f = chebfun(@(x) sin(x)+cos(3.3*x)-0.5*x.^2, [0 2])
f =
chebfun column (1 smooth piece)
interval          length   values at Chebyshev points
(        0,        2)        21            1          1          1 ...      -0.14



You can see that this function is represented by 21 points, i.e. a polynomial of degree 20. Let's plot it, along with the intermediate points (Chebyshev points)

plot(f, '.-')

This chebfun representation can be evaluated at arbitrary points:

f([0.2, 0.5, 0.9, 1.5, 1.85])
ans =
0.9687    0.2753   -0.6070    0.1079    0.2342


To calculate the integral from 0 to 2,

sum(f)
ans =
0.1772


The roots can be found by

r = roots(f)
hold on
plot(r, f(r), 'ro', 'MarkerFaceColor', 'r');
r =
0.5953
1.4429
1.9557


By now, you probably have noticed that chebfun is implemented using object-oriented programming, with a number of overloaded methods, such as plot, sum, and roots.

I can say for sure that I'm not doing justice to this entry with this short blog post. I've just scratched the surface with what could be done with this tool. I highly encourage you to take a look through the documentation and give it a try. On the download page, it says that they have Version 4 available for download off of their university website. I'm hoping that they will also update the version on the File Exchange.

Get the MATLAB code

Published with MATLAB® 7.13

### Note

bazli replied on : 1 of 4

a = magic(3);
sum(a)

i want to ask if you can help me how to process continous data (x,y,z). my project is monitoring survey, so i want to know how to calculate the magnatidue for (x,y,z) using matlab

Vassili replied on : 2 of 4

Hi Jiro!

Sorry if the following question is stupid.

Assume that instead of an analytical expression for a cheb-function (as in your example) I have only vectors X and Y, vector Y being the values of the wanted function f(x). For x-values different from X one can define function f via interpolated values. Question: does the new CHEBFUN give this possibility, e.g.

f=chebfun(@(x) Y,X)

?

This would essentially increase the scope of practical applications of CHEBFUN

Sincerely
Vassili

Jiro Doke replied on : 3 of 4

@bazil,
I’m not sure what you are asking. But it doesn’t sound very relevant to this particular post. Perhaps what you’re looking for is the abs function. You can also try asking the question on MATLAB Answers.

@Vassili,
You may get a more complete answer if you post your question on the File Exchange entry page. But my understanding is that Chebfun is meant for solving analytical problems, using numeric power. If the data is already numeric (data points), then it’s probably more efficient to use the builtin numeric functions.

Having said that, this is my attempt to make use of chebfun using spline:

x = 0:.5:10; y = sin(1.2*x) - 0.5*cos(3*x); f = chebfun(@(xx) spline(x,y,xx), [0, 10]) plot(f) % plot chebfun hold on; plot(x, y, 'ro'); % plot original points r = roots(f); % this is slow plot(r, zeros(size(r)), 'k*'); 

Vassili replied on : 4 of 4

Dear Jiro !!!

“my understanding is that Chebfun is meant for solving analytical problems, using numeric power.”

Exactly this was my impression, too. So thanks for finding my question not too stupid.

As to your suggestion: it is very close to an exact answer. Why only close? Because sufficiently high sampling frequency needs higher accuracy than SPLINE can produce, therefore the exact answer should be based on the choice of the degree of interpolating and therefore chebfun-defining set of cheb-polynomials.

Nice weekend to you and colleagues!!!