英文:
how to create a data visualization with a function in Matlab
问题
我正在尝试研究一个无线通信教材中的一些代码,其中我有一段来自教材的代码:
function PL=PL_free(fc,dist,Gt,Gr)
% 自由空间传输损耗模型
% 输入参数
% fc : 载波频率[Hz]
% dist : 基站与移动站之间的距离[m]
% Gt : 发射天线增益
% Gr : 接收天线增益
% 输出
% PL : 传输损耗[dB]
lambda = 3e8/fc;
tmp = lambda./(4*pi*dist);
if nargin > 2
tmp = tmp*sqrt(Gt);
end
if nargin > 3
tmp = tmp*sqrt(Gr);
end
PL = -20*log10(tmp);
在教材中,还有一个基于这个Matlab代码生成的图表。我的问题是,如何根据这个函数自己创建这个图表?我是否需要为所有变量提供数据来源?如果我无法获取数据来源,比如Excel文件,是否有Matlab内置工具支持创建这个图表?这个图表类似于这样:
(我了解绘图的概念,它在命令中,输入一些函数的数据,但它只会显示函数输出的答案。我无法在网上找到合适的解决方案,所以我来这里寻求帮助)
(注意:您需要提供载波频率 fc
、距离 dist
、发射天线增益 Gt
和接收天线增益 Gr
的值,以便使用该函数创建图表。您可以手动提供这些值作为输入参数。)
英文:
I am trying to study some codes inside a wireless communication textbook, which I have a piece of code from the textbook:
function PL=PL_free(fc,dist,Gt,Gr)
% Free Space Path loss Model
% Input
% fc : carrier frequency[Hz]
% dist : between base station and mobile station[m]
% Gt : transmitter gain
% Gr : receiver gain
% output
% PL : path loss[dB]
lamda = 3e8/fc;
tmp = lamda./(4*pi*dist);
if nargin>2
tmp = tmp*sqrt(Gt);
end
if nargin>3
tmp = tmp*sqrt(Gr);
end
PL = -20*log10(tmp);
And inside the textbook, there is also a graph that is generated based on this Matlab code. My question is, how can I create this graph based on the function myself? Do I need to have a data source for all of the variables? If I cannot get a data source like an excel file, is there any way inside Matlab there is a built-in tool that supports creating this graph? The graph is like this:
(I understand the concept of the plot which is in the command, enter some data of the function, but it will only show the answer of the function output. I cannot find a suitable solution anywhere online so here I come)
答案1
得分: 0
fc = __ % 选择值
Gt = __ % 选择值
Gr = __ % 选择值
dist = logspace(0, 3, 16);
PL=PL_free(fc,dist,Gt,Gr);
semilogx(dist, PL, 'ro')
或者:
dist = logspace(0, 3, 16);
PL = zeros(1, numel(dist));
for ii = 1:numel(dist)
PL(ii) = PL_free(fc, dist, Gt, Gr);
end
semilogx(dist, PL, 'ro')
我不确定在这里使用linspace
还是logspace
更好。linspace
创建具有等间距值的向量,而logspace
创建具有对数间距值的向量。要绘制更多值,您可以使用hold on
或hold all
,并使用新的PL
进行semilogx
。
英文:
I haven't testet this, but something along the lines of:
fc = __ % Choose value
Gt = __ % Choose value
Gr = __ % Choose value
dist = logspace(0, 3, 16);
PL=PL_free(fc,dist,Gt,Gr);
semilogx(dist, PL, 'ro')
Alternatively:
dist = logspace(0, 3, 16);
PL = zeros(1, numel(dist));
for ii = 1:numel(dist)
PL(ii) = PL_free(fc, dist, Gt, Gr);
end
semilogx(dist, PL, 'ro')
I'm not sure if linspace
or logspace
is best here.linspace
creates a vector with equally spaced values, while logspace
creates a vector with logarithmically spaced values. To plot more values you can do hold on
or hold all
, and do semilogx
with a new PL
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论