View Full Version : Learning Matlab

Hal Jordan
September 30th, 2012, 03:30 AM
Hi, it's my first time on these forums. I've used Ubuntu before but my question today isn't about Ubuntu, it's about Matlab. I read somewhere that all kinds of languages are discussed on these forums so I decided to try my luck here. My instructor assigns such long projects every week that are very time-consuming but does not really take the time to explain the subject.

I want to write a program that plots one peicewise function
f(t) = -Pi*t on −1 ≤ t ≤ 0 and Pi*t on 0 < t ≤ 1 and one Fourier Series F(t) = Pi/2 - 4/Pi Sum[(1/2k+1)*Pi*t*cos(2k-1)] k = 1 to Infinity on −1 ≤ t ≤ 1 using the fplot command. It's only my fourth assignment using Matlab so it's a bit of a jump going from one plot to two or more on the same graph. I wanted to show that I made an attempt at the problem so here is my code so far:

function Ft = Fourier(t)
% Ft = Fourier(t)
% t: Values between -1 and 1
% Ft: Fourier approximation
term(i)=1./(((2.*a(i)-1.^2)).*cos ((2.*a(i)-1)).*pi.*t);

Any help would be appreciated.

October 2nd, 2012, 10:34 AM
I did not check what your program should do - probably it prints a function in [-1 1] and then, some of its harmonics ...

Here's the code (it MIGHT help if you check the documentation of fplot carefully ;) )

function run_this_thing % the main function
t = [-1 1]; % define f,F interval
fh = @FT; % function handle
fplot(fh, t); % do the plot

function Y = FT(t) % vectors to plot in
Y(:,1) = sign(t(:)) * pi;% this is f(t)
for k = 2: 1: 10 % number of k's ...
Y(:,k) = pi/2 - 4/pi*sum((1/2*k+1)*pi*t*cos(2*k-1)) ; % this is F(t)