<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Matlab</title>
        <link>http://blogs.ugidotnet.org/Andrew/category/Matlab.aspx</link>
        <description>Matlab</description>
        <language>it-IT</language>
        <copyright>Andrea Sansottera</copyright>
        <generator>Subtext Version 2.6.0.0</generator>
        <item>
            <title>[OT: Matlab] Calcolo e la rappresentazione di una serie di Fourier</title>
            <link>http://blogs.ugidotnet.org/Andrew/archive/2005/07/06/22280.aspx</link>
            <description>Matlab &amp;#232; un ambiente destinato al calcolo numerico, alla simulazione e allo sviluppo di modelli matematici. Il linugaggio Matlab &amp;#232; dinamico; i tipi base non sono valori scalari, bens&amp;#236; matrici. Il codice &amp;#232; JIT-compilato, la memoria &amp;#232; gestita da un garbage collector.
Durante la preparazione dell'esame di Analisi 2 - presso il Politecnico di Milano - ho sviluppato questa semplice routine per il calcolo e la rappresentazione della serie di Fourier associata ad una funzione. La pubblico con la speranza che possa essere utile ad altri studenti e interessare chi non ha mai utilizzato l'ambiente Matlab.
%%   DRAWFOURIER%% Definition:%   function [xvalues, yvalues] = drawfourier(y, x, from, to, step, precision)%% Description:%   Draws the graphic of the function y(x) and the graphic of its Fourier's%   series. Requires the Symbolic Math Toolbox.% % Arguments:%   y:              symbolic expression of the function%   x:              symbolic variable rappresenting the independent variable%   from:           left boundary of the period%   to:             right boundary of the period%   step:           the distance beetween two points in which the series is computed%   precison:       the maximum index of the series% % Returned values:%   xvalues:        the values in the domain of the Fourier's series%   yvalues:        the values of the Fourier's series%% Example:%   x = sym('x'); drawfourier(x^2, x, -pi, pi, 0.1, 50);%% Information:%   Author:         Andrea Sansottera (andrea.sansottera@fastwebnet.it)%   Date:           June 20th 2005%   Version:        0.6%   License:        GPLfunction [xvalues, yvalues] = drawfourier(y, x, from, to, step, precision)% starts the stopwatchtic;% initializes the symbolic index of the seriesk = sym('k');% initializes period and pulsationt = (to - from);c = 2/t;omega = (2*pi)/t;% computes the Fourier coefficients of the seriesa0 = c * int (y, from, to);ycos = y * cos(omega*k*x);ak = c * int(ycos, from, to);ysin = y * sin(omega*k*x);bk = c * int(ysin, from, to);% defines the domain of the seriesxvalues = [from : step : to];% foreach point in the domain of the functionfor i = [1 : length(xvalues)]    % computes the value of the Fourier's series    yvalues(i) = subs(a0/2);    for kval = 1 : precision        yvalues(i) = yvalues(i) + subs(ak*cos(omega*k*x), {k, x}, {kval, xvalues(i)});        yvalues(i) = yvalues(i) + subs(bk*sin(omega*k*x), {k, x}, {kval, xvalues(i)});    endend% plots the functionezplot(y, [from, to]);% keeps the graphic on the figurehold on;% plots the Fourier's seriesplot(xvalues, yvalues, 'r');% sets the labels of the figurexlabel('x');ylabel('y');% sets the title of the figurefigure_title = sprintf('%s  period = [%.2f, %.2f]   step = %.2f   k = [1, %d]', char(y), from, to, step, precision);title(figure_title);% displays a legendseries_label = sprintf('Fourier''s series of %s', [char(y)]);legend(char(y), series_label);% setups axisaxis([from to min(yvalues) max(yvalues)]);% stops the stopwatch and displays the elapsed timetoc;end
Ecco un esempio. Disegniamo la parabola di equazione x^2 e la sua serie di Fourier, nell'intervallo [0, 2*pi], valutandola ogni 0.05. Consideriamo solamente i primi 5 termini della serie.
syms x; drawfourier (x^2, x, 0, 2*pi, 0.05, 5)

Essendo la funzione limitata e monotona a tratti, sono soddisfatte le ipotesi del teorema di convergenza puntuale. Cerchiamo di sfruttarlo per ottenere una migliore approssimazione. Scegliamo un intervallo in cui la funzione presenti simmetria pari: in tale intervallo soddisfer&amp;#224; la condizione di raccordo. Poich&amp;#232; la funzione &amp;#232; continua la serie di Fourier converge in ogni singolo punto alla y(x). Si noti come in questo modo l'approssimazione migliori, pur sottoponendo la macchina allo stesso carico computazionale (il termini valutati sono sempre i primi 5).
syms x; drawfourier (x^2, x, -pi, +pi, 0.05, 5)

Com'&amp;#232; facilmente intuibile, aumentando il numero di termini della serie valutati, la precisione migliora ulteriormente. syms x; drawfourier (x^2, x, 0, 2*pi, 0.05, 20) 

 &lt;img src="http://blogs.ugidotnet.org/Andrew/aggbug/22280.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Andrea Sansottera</dc:creator>
            <guid>http://blogs.ugidotnet.org/Andrew/archive/2005/07/06/22280.aspx</guid>
            <pubDate>Wed, 06 Jul 2005 13:11:00 GMT</pubDate>
            <comments>http://blogs.ugidotnet.org/Andrew/archive/2005/07/06/22280.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://blogs.ugidotnet.org/Andrew/comments/commentRss/22280.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>