[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Useless functions in matlab function library.
From: |
Qingshan Chen |
Subject: |
Re: Useless functions in matlab function library. |
Date: |
Tue, 10 Jul 2007 18:34:22 -0400 |
User-agent: |
Icedove 1.5.0.12 (X11/20070607) |
I want to do one thing, and I found the way how to do it with inline.
Here is a little script:
%%%%%%%%%%%%Script starts%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This script explore the fourier series expansion of a given
% function over the interval [x0,x1]. by plot the approximation series
(to a given degree).
n=10; x0=-1; x1=1; dx=0.01;
tol=[1.0e-4,0];
thefunction = '(exp(x)+exp(-x))/2';
f = inline(thefunction);
x=x0:dx:x1; fx=zeros(1,size(x,1));
a=zeros(n,1); b=zeros(n,1);
a0=quad(f,x0,x1, tol)/(x1-x0);
len=x1-x0;
for k=1:n
f1=@(x) f(x).*sin(2*k*pi*x/len);
f2=@(x) f(x).*cos(2*k*pi*x/len);
a(k) = quad(f1,x0,x1, tol)/(0.5*len);
b(k) = quad(f2,x0,x1, tol)/(0.5*len);
end
fx=a0;
for k=1:n
fx = fx + a(k)*sin(2*k*pi*x/len) + ...
b(k)*cos(2*k*pi*x/len);
end
plot(x,fx, x, f(x));
title(['Fourier series expansion of the function ' thefunction]);
%%%%%%%%%%%%%%%%%%%%%Script ends%%%%%%%%%%%%%%%%%%%%%%%
Basically this script find and plot the fourier expansion of a given
function, and then title
the plot with a string expression for the function. To plot another
function, you just need to
change the string value for the variable thefunction, and at the end the
expression for the
function will be updated in the title automatically. Can you do this
with anonymous function only?
If yes, tell me how. I am quite new to both octave and matlab. So if you
find me ignorant, please
don't shout at me:)
Guillem Borrell Nogueras wrote:
I'm so sorry if starting a discussion in a help mailing list is completely
inappropiate...
Thinking about the previous thread...
Is the inline function just a bad, syntactically wrong anonymous function?
I've always criticized Matlab saying that it needs to seriously sanitize the
language. I find the inline function completely wrong, it's like an eval
function used to assign a function to a variable. And David just said that it
is even slow! (well, in Matlab). Is the inline function used for any other
purpose than something that is much better done with an anonymous function?
(inlining code is a much broader concept)
I taught a bit of matlab in my school of engineering. I had only six hours to
taught as much matlab as possible and I never told my students about the
inline function. Instead, I spent more than an hour trying to make them
understand what a function handle is.
Do you know any matlab function that is just rubbish? What parts of matlab
you think that are completely wrong (like the object orientation
implementation)?
guillem
On Tuesday 10 July 2007 12:35, David Bateman wrote:
Guillem Borrell Nogueras wrote:
It is much better to use an anonymous function to fit this purpose.
In this case would be:
k = 2
f = @(x) k*x^2
Well, for Matlab this is true, as the inline functions use Matlab's
object class whereas anonymous functions are in the matlab
parser/interpreter itself. So under Matlab anonymous functions are
significantly faster to use than inline functions. However, under Octave
inline functions are just a special type of anonymous functions and so
there is no speed disadvantage.
That being said, yes it is still better to use anonymous functions as
then you'll have better performance if you share your code with users of
the other product... Also, the way "inline" finds what are the arguments
of the function are, is a horrible mess which Octave had to copy for
compatibility. Anonymous functions have a much cleaner interface (i.e.
you are required to define the arguments)..
D.
To make this thing work you must upgrade to octave 2.9.x. In earlier
versions of octave the anonymous function won't find the constant k.
You can find more information about anonymous functions and function
handles here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html. look
for anonymous functions in the index.
guillem
_______________________________________________
Help-octave mailing list
address@hidden
https://www.cae.wisc.edu/mailman/listinfo/help-octave
- independent variable in function for leasqr, oxyopes, 2007/07/08
- Re: independent variable in function for leasqr, James Sherman Jr., 2007/07/08
- Re: independent variable in function for leasqr, oxyopes, 2007/07/09
- Re: independent variable in function for leasqr, James Sherman Jr., 2007/07/09
- Re: independent variable in function for leasqr, oxyopes, 2007/07/10
- Re: independent variable in function for leasqr, David Bateman, 2007/07/10
- Re: independent variable in function for leasqr, Guillem Borrell Nogueras, 2007/07/10
- Re: independent variable in function for leasqr, David Bateman, 2007/07/10
- Useless functions in matlab function library., Guillem Borrell Nogueras, 2007/07/10
- Re: Useless functions in matlab function library.,
Qingshan Chen <=
- Re: Useless functions in matlab function library., Jordi GutiƩrrez Hermoso, 2007/07/10
- Re: Useless functions in matlab function library., Guillem Borrell Nogueras, 2007/07/11
- Re: Useless functions in matlab function library., Paul Kienzle, 2007/07/11