How to vectorize the function?

3 ビュー (過去 30 日間)
Xizeng Feng
Xizeng Feng 2021 年 11 月 28 日
コメント済み: Xizeng Feng 2021 年 11 月 28 日
I run the following codes and had the result of fplot. But comes also some warning as in the bottom,
Can anyone tell me how to improve my codes, or how to vectorize my function?
fc=1e+8; % carrier
fm=1e+3; % modulate
fd=7.5e+4; % frequency deviation
r=0.25; t0=0.0005; % reflection and delay
w0=2*pi*fc; fm=2*pi*fm; % changing to angular frequenvy
out=@(t)(fd*sin(fm*t)-fd*sin(fm*(t-t0)))...
*(r^2+r*(cos(w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))...
/(1+r^2+2*r*cos((w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))+fd*sin(fm*t);
fplot(out,[0 .003])
------------------------------
the warnings:
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 237)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 192)
In fplot>vectorizeFplot (line 192)
In fplot (line 162)
In test1 (line 10)

採用された回答

per isakson
per isakson 2021 年 11 月 28 日
編集済み: per isakson 2021 年 11 月 28 日
Read Array vs. Matrix Operations. With scalars the two produce the same result, but not with vectors and arrays.
The second section of the script is the vectorize version.
%%
fc=1e+8; % carrier
fm=1e+3; % modulate
fd=7.5e+4; % frequency deviation
r=0.25; t0=0.0005; % reflection and delay
w0=2*pi*fc; fm=2*pi*fm; % changing to angular frequenvy
%%
t = linspace( 0, 0.003, 1000 );
y = (fd.*sin(fm.*t)-fd.*sin(fm.*(t-t0))) ...
.*(r^2+r.*(cos(w0.*t0+fd.*cos(fm.*t)./fm-fd.*cos(fm.*(t-t0))./fm))) ...
./(1+r^2+2.*r.*cos((w0.*t0+fd.*cos(fm.*t)./fm-fd.*cos(fm.*(t-t0))./fm)))+fd.*sin(fm.*t);
figure
plot( t, y )

その他の回答 (1 件)

Chunru
Chunru 2021 年 11 月 28 日
fc=1e+8; % carrier
fm=1e+3; % modulate
fd=7.5e+4; % frequency deviation
r=0.25; t0=0.0005; % reflection and delay
w0=2*pi*fc; fm=2*pi*fm; % changing to angular frequenvy
% Change * and / into .* and ./
out=@(t) (fd*sin(fm*t)-fd*sin(fm*(t-t0)))...
.*(r^2+r*(cos(w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))...
./(1+r^2+2*r*cos((w0*t0+fd*cos(fm*t)/fm-fd*cos(fm*(t-t0))/fm)))+fd*sin(fm*t);
fplot(out,[0 .003])
  1 件のコメント
Xizeng Feng
Xizeng Feng 2021 年 11 月 28 日
thank you very much for your answer!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by