Advice using function titles with abs command and plotting with function command

6 ビュー (過去 30 日間)
Jai Harnas
Jai Harnas 2021 年 3 月 25 日
編集済み: Jan 2021 年 3 月 25 日
Hi all, continuing my code for a fourier series assignment and have hit a brick wall surrounding the function handles and the abs command. Since my last question i have changed the layout of the code and created a single function from the piecewise function i originally had. However, adding this in seperately it was plotting fine (apart from a few data cursor errors when moving the plot(error updating pointdatatip), but then adding the second fplot line stops the first from plotting. Additionally, the errors i am recieving are exclusively related to the first fplot line.
My code is below:
clear
ft=@f;
w=2;
T=(2*pi)/w;
hold on
fplot(ft,[-pi,pi])
%evaluating C0
c0=(1/T)*integral(ft,-T/2,0)+(1/T)*integral(ft,0,T/2);
%evaluating Cn values up to n=10 from n=1
for n = 1:10
f3=@(t) ((4+t)/2).*exp(-1j*n*w*t);
f4=@(t) ((2-t).*cos(2*t)).*exp(-1j*n*w*t);
c(n,1)=(1/T)*integral(f3,-T/2,0)+(1/T)*integral(f4,0,T/2);
end
%turning values n=1:10 to conjugates
cc=conj(c);
%series addition for k=-10:10
k=(1:10).';
s=@(t) c0+sum(c(k).*exp(1j*k*w*t),1)+sum(cc(k).*exp(1j*-1*k*w*t),1);
fplot(s,[-pi,pi],'r')
%evaluating the average energy of the signal
f5=@(t) abs((4+t)/2).^2;
f6=@(t) abs((2-t).*cos(2*t)).^2;
E=(1/T)*integral(f5,-T/2,0)+(1/T)*integral(f6,0,T/2);
Above and below this point i would prefer to use the abs(ft).^2 above, however, this works just fine
I am not sure how to implement the line abs(ft-s) below, as they might need scalar values or arrays?
z1=0;
z2=0;
x=0;
while z2<1/10
s2=@(t) c0+sum(c(x).*exp(1j*x*w*t),1)+sum(cc(x).*exp(1j*-1*x*w*t),1);
x=+1;
z1=ft-s2;
z2=abs(z1);
end
function y=f(t)
if (-pi<t)&(t<0)
y=(4+t)/2;
elseif (0<=t)&(t<pi)
y=(2-t).*cos(2*t);
end
end
Thanks in advance for any help given
  4 件のコメント
Jan
Jan 2021 年 3 月 25 日
編集済み: Jan 2021 年 3 月 25 日
Where does this warning occur?
It is still not clear to me, what your problem is.
Jai Harnas
Jai Harnas 2021 年 3 月 25 日
The warning is exclusive to the first fplot line and im not sure how to fix it

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

回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 3 月 25 日
s2=@(t) c0+sum(c(x).*exp(1j*x*w*t),1)+sum(cc(x).*exp(1j*-1*x*w*t),1);
x=+1;
z1=ft-s2;
s2 is a function handle. You need to invoke it with a parameter.
x=+1;
You are assigning positive 1 to x, not adding 1 to x.
  3 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 25 日
No, you cannot take the difference between function handles. A function handle is a pointer to a block of memory where the struct() is that holds the information about the function handle. If there were a meaning for subtraction it would mean how far apart the data structures are in memory.
You can invoke a function handle on a value to get a numeric result. However, you would need a definite t to invoke the handle on, and based on what you have posted, you do not have a definite t.
Jai Harnas
Jai Harnas 2021 年 3 月 25 日
Okay thank you, thats what i was wondering, is there anyway to overcome this or would i have to re-write my code in another way with a definite value for t?

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


Jan
Jan 2021 年 3 月 25 日
If f the values t == pi and t == -pi are excluded also. Is this wanted?
Do you want f to accept vectors for t? Then:
function y = f(t)
y = zeros(size(t));
index = (-pi<t) & (t<0);
y(index) = (4 + t(index)) / 2;
index = (0<=t) & (t<pi)
y(index) = (2 - t) .* cos(2 * t);
end
The IF condition must be a scalar, so if pi<t is converted implicitly to if all(pi < t).
  2 件のコメント
Jai Harnas
Jai Harnas 2021 年 3 月 25 日
Accepting scalars for what i am trying to do throughout this code is fine. Implementing this code however, still gives the error telling me that the function behaves unexpectedly
Jan
Jan 2021 年 3 月 25 日
編集済み: Jan 2021 年 3 月 25 日
@Jai Harnas: Please do not rephrase the error message in your word, but post a copy of the complete message. Then it woulöd start to get clear, in which part of the code the problem is.
"Accepting scalars for what i am trying to do throughout this code is fine." - I do niot think so. You get a corresponding error message. In you original code you provide the interval [-pi, pi], but for t==pi and t==-pi the function f does not reply an output.

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by