Help with Matlab-Chebyshev


lacy1104
04-28-2008, 11:28 AM
1. Write the following MATLAB function
function [C,X, Y ] = Chebyshev (func, N, a, b)
to construct and evaluate an interpolating polynomial of degree N for f(x) over [a, b], with the zeros of the Chebyshev polynomial of degree N + 1 as nodes.

Inputs: func - the string function to be approximated
a - left end point
b - right end point
N - the degree of the interpolating polynomial
Outputs: C - the vector containing the coefficients of the interpolating polynomial
X - the abscissa vector
Y - the ordinate vector
Results are to be desplayed in this fashion:

Tables for the results
x f(x) y f(x) − y
0.5
0.8
0.9
0.99


Data and Results for Problem # 1
1. f(x) = ln(x + 3) over [−1, 1]
N = 3
2. f(x) = sin x + cos x over [−1, 1]
N = 2
3. f(x) = cos x over [−1, 1].
N = 2

I am really new to matlab and computer programming in general. Can anyone help me with this.Thank you

lacy1104
05-06-2008, 06:00 PM
So far this is what i did, but how do you call a function?

function [C,X,Y] = Chebyshev( func, N, a, b )

X=(b+a-(b-a)*cos((2*(0:N-1)+1)*pi/(2*N)))/2;
Y = eval(func,X);

C=zeros(1,N+1);


call function as
[C,X,Y] = Chebyshev( ['cos(X)'], 2, -1, 1)

davekw7x
05-07-2008, 12:14 PM
So far this is what i did,...


If you pass the function as a string, then you can use it as an inline function. Of course your function isn't complete (yet), but you can see how it can work by doing something like this:

function [C,X,Y] = Chebyshev(func, N, a, b)
X=(b+a-(b-a)*cos((2*(0:N-1)+1)*pi/(2*N)))/2
f = inline(func)
Y = f(X)
C=zeros(1,N+1);


Since I didn't put semicolons at the end of any of the first three assignments, you can see their effects.
Call it with

[C, X, Y] = Chebyshev('cos(x)', 3, -1, 1);


And you might see something like:

X =

-8.6603e-01 -6.1230e-17 8.6603e-01

f =

f(x) = cos(x)

Y =

0.64786 1.00000 0.64786


At least that's what I see with octave.

Regards,

Dave

lacy1104
05-07-2008, 08:52 PM
If you pass the function as a string, then you can use it as an inline function. Of course your function isn't complete (yet), but you can see how it can work by doing something like this:

function [C,X,Y] = Chebyshev(func, N, a, b)
X=(b+a-(b-a)*cos((2*(0:N-1)+1)*pi/(2*N)))/2
f = inline(func)
Y = f(X)
C=zeros(1,N+1);


Since I didn't put semicolons at the end of any of the first three assignments, you can see their effects.
Call it with

[C, X, Y] = Chebyshev('cos(x)', 3, -1, 1);


And you might see something like:

X =

-8.6603e-01 -6.1230e-17 8.6603e-01

f =

f(x) = cos(x)

Y =

0.64786 1.00000 0.64786


At least that's what I see with octave.

Regards,

Dave

So, let me get this straight. When i pass the function as a string i have to put
function [C,X,Y] = Chebyshev(func, N, a, b)
X=(b+a-(b-a)*cos((2*(0:N-1)+1)*pi/(2*N)))/2
f = inline(func)
Y = f(X)
C=zeros(1,N+1);

and
[C, X, Y] = Chebyshev('cos(x)', 3, -1, 1);
on the same page and that should produce answer like what you got?

davekw7x
05-07-2008, 10:21 PM
So, let me get this straight...

I just took your code for the function Chebyshev and added the stuff about inline.

I saved those five lines of code in a file named Chebyshev.m in the directory from which I invoked octave.

Then at the prompt in octave, I entered the function call, and I showed you what I saw using octave version 3.0.0 on my Centos 5.1 Linux workstation.

The three values of X are the Chebyshev nodes (plus or minus roundoff error), and each value printed for Y is the cosine of the corresponding value of X, right?

The whole point of my post was to try to answer your question about using a function as an argument to a function, which is what you apparently need.


So: what did you see from Matlab? I thought that it should be about the same, but I have no way of testing.

Unlike Matlab, octave is free, you know. Matlab has lots more goodies, and some of the octave graphics interface functions and features aren't exactly the same as with Matlab (Matlab doesn't depend on gnuplot and is probably more powerful for many applications), but basic operations and commands should be the same, I'm thinking.


Regards,

Dave

lacy1104
05-08-2008, 07:56 AM
And you might see something like:

X =

-8.6603e-01 -6.1230e-17 8.6603e-01

f =

f(x) = cos(x)

Y =

0.64786 1.00000 0.64786


At least that's what I see with octave.

Regards,

Dave


In Matlab this is what i got:
>> [C, X, Y] = Chebyshev('cos(x)', 3, -1, 1);

X =

-0.8660 -0.0000 0.8660


f =

Inline function:
f(x) = cos(x)


Y =

0.6479 1.0000 0.6479

Thank you so much for your help!