plotting a function using a function

12 Ansichten (letzte 30 Tage)
jvfa
jvfa am 9 Sep. 2023
Bearbeitet: jvfa am 14 Sep. 2023
Hello everyone, I like to ask how do i plot a function that integrates a certain function
Cn = integral(function of the sawtooth * exp(-1i*2*pi*n*f*t), 0, T);
so far this is my working code which only plots the sawtooth wave function. I have made multiple changes to it trying to figure out how to plot Cn. I'm still fairly new to matlab. maybe I'm missing something.
close all; figure; t=linspace(0,3,1500); m=linspace(0,3,1500); T=1; f=1/T; amp=1; sawf=amp/2; k=plot(NaN,NaN); 1i; for n=1:1:500 sawf = sawf - (amp/pi)*(1/n)*(sin(2*pi*n*f*t)); set(k, 'XData',t,'YData',sawf); pause(0.01); end
%cn1= integral(sawf*((cos(2*pi*1*f*t))-(-(1i)*sin(2*pi*1*f*t))),0,T); %cn1= sawf*(sin(2*pi*n*f*t)/(2*pi*n*f))+((i)cos(2*pi*n*f*t)/(2*pi*n*f)); %cn = (1./T)*cn1; %set(k, 'XData',m,'YData',cn);
  3 Kommentare
Dyuman Joshi
Dyuman Joshi am 9 Sep. 2023
Bearbeitet: Dyuman Joshi am 9 Sep. 2023
integral requires a function handle as an integrand. You have not supplied any function, just a numerical value.
Also, I don't understand how this -
%1
cn1= integral(sawf*((cos(2*pi*1*f*t))-(-(1i)*sin(2*pi*1*f*t))),0,T);
results in this -
%2
cn1= sawf*(sin(2*pi*n*f*t)/(2*pi*n*f))+((i)cos(2*pi*n*f*t)/(2*pi*n*f));
or how did you arrive at line 2.
or was it supposed to be n instead of 1 (next to pi inside the trignometric terms) in line 1?
jvfa
jvfa am 9 Sep. 2023
Hi, can you elaborate where i where i got wrong on what you said about integral? because it's really a big part of understanding the matlab function of integration that I lack.
those comments don't worry about them they were just my attempts at understanding the function. %1 was originally n then i tried substituting 1 for n to see what the value is sort of doing trial and error. for %2 i manually integrated the thing by hand, given that i have the value for sawf so i can move it out of the integral sign then just integrated exp(-2i*pi*n*f*t)dt instead

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 9 Sep. 2023
Bearbeitet: Bruno Luong am 9 Sep. 2023
The coefficients Cn = 1i/(2*pi*n).
I'm not very good in symbolic calculation, I never own the tollbox license.
syms t
syms n integer
sawtooth = t;
Cn = simplify(int(sawtooth*exp(-1i*2*pi*n*t), t, 0, 1))
Cn = 
C0 = simplify(int(sawtooth, t, 0, 1)) % funny the above general formula is wrong for n=0
C0 = 
Cnfun = matlabFunction(Cn);
n = -10:10;
Cnval = Cnfun(n);
Cnval(n==0) = subs(C0); % no idea why I need to do this
figure
hold on
plot(n, real(Cnval))
plot(n, imag(Cnval))
legend('real','imag')
xlabel('n')
ylabel('C(n)')
t = linspace(0,3,1000)';
f = @(t) mod(t,1)
f = function_handle with value:
@(t)mod(t,1)
fFourier = sum(Cnval.*exp(1i*2*pi*n.*t),2);
figure
plot(t, f(t), 'r', t, fFourier, 'b')
Warning: Imaginary parts of complex X and/or Y arguments ignored.
xlabel('t')
legend('f', 'Fourier approximation')
  5 Kommentare
Bruno Luong
Bruno Luong am 9 Sep. 2023
@Paul thanks
jvfa
jvfa am 10 Sep. 2023

hi bruno, after studying your comment and code i finally understand. thank you, alsp for paul for the correction

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by