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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by