plotting a function using a function

15 ビュー (過去 30 日間)
jvfa
jvfa 2023 年 9 月 9 日
編集済み: jvfa 2023 年 9 月 14 日
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 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 9 日
編集済み: Dyuman Joshi 2023 年 9 月 9 日
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 2023 年 9 月 9 日
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

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 9 月 9 日
編集済み: Bruno Luong 2023 年 9 月 9 日
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 件のコメント
Bruno Luong
Bruno Luong 2023 年 9 月 9 日
@Paul thanks
jvfa
jvfa 2023 年 9 月 10 日

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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by