How to vectorize the function?

3 Ansichten (letzte 30 Tage)
Xizeng Feng
Xizeng Feng am 28 Nov. 2021
Kommentiert: Xizeng Feng am 28 Nov. 2021
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)

Akzeptierte Antwort

per isakson
per isakson am 28 Nov. 2021
Bearbeitet: per isakson am 28 Nov. 2021
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 )

Weitere Antworten (1)

Chunru
Chunru am 28 Nov. 2021
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 Kommentar
Xizeng Feng
Xizeng Feng am 28 Nov. 2021
thank you very much for your answer!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by