Int function not working

9 ビュー (過去 30 日間)
Jai Harnas
Jai Harnas 2021 年 3 月 19 日
コメント済み: Jai Harnas 2021 年 3 月 19 日
Hi all, im relatively new to the whole MATLAB thing and was wondering why my below code isnt working and giving me the error:
Check for missing argument or incorrect argument data type in call to function 'int'.
Error in MME32 (line 17)
c0=(1/T)*int(f1,T/2,0)+(1/T)*int(f2,0,T/2)
My Code:
clear
t1=-pi:0.001:0;
t2=0:0.001:pi;
w=2;
T=(2*pi)/w;
f1=(4+t1)/2;
f2=(2-t2).*cos(2*t2);
hold on
plot(t1,f1)
plot(t2,f2)
c0=(1/T)*int(f1,-T/2,0)+(1/T)*int(f2,0,T/2)
i also have the line of code:
c(n)=(1/T)*int(@f1.*exp(-j*n*w*t1),T/2,0)+(1/T)*int(@f2.*exp(-j*n*w*t2,0,T/2)
that doesnt work either.
Thanks in advance!
  2 件のコメント
Jan
Jan 2021 年 3 月 19 日
編集済み: Jan 2021 年 3 月 19 日
Whenever you mention an error in the forum, post a copy of the complete error message. "Does not work" ist not clear enough to understand, what the problem is.
f1 is a numerical vector. Then "@f1" does not create symbolic expression and not a function handle also.
Jai Harnas
Jai Harnas 2021 年 3 月 19 日
Okay, i will do that in future thank you

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 19 日
You have no symbolic variables or symbolic functions. int() is only used for symbolic expressions or symbolic functions.
For numeric integration, there are several routines, the main two of which are integral() and integral2(). However, for those you need function handles.
w=2;
T=(2*pi)/w;
f1 = @(t) (4+t)/2;
f2 = @(t) (2-t).*cos(2*t);
fplot(f1, [-T/2, 0]);
hold on
fplot(f2, [0, T/2]);
c0 = (1/T)*integral(f1,-T/2,0) + (1/T)*integral(f2,0,T/2)
c0 = 0.9628
  1 件のコメント
Jai Harnas
Jai Harnas 2021 年 3 月 19 日
Thank you this works perfectly! i did try something similar to this but had things around the wrong way and all messed up. Thank you !

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

その他の回答 (1 件)

Jan
Jan 2021 年 3 月 19 日
See:
doc int
The 1st argument must be: symbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic number
You provide a numerical vector instead. Then trapz is the right tool for an integration.

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by