在Matlab中如何使用函数创建数据可视化。

huangapple go评论78阅读模式
英文:

how to create a data visualization with a function in Matlab

问题

我正在尝试研究一个无线通信教材中的一些代码,其中我有一段来自教材的代码:

  1. function PL=PL_free(fc,dist,Gt,Gr)
  2. % 自由空间传输损耗模型
  3. % 输入参数
  4. % fc : 载波频率[Hz]
  5. % dist : 基站与移动站之间的距离[m]
  6. % Gt : 发射天线增益
  7. % Gr : 接收天线增益
  8. % 输出
  9. % PL : 传输损耗[dB]
  10. lambda = 3e8/fc;
  11. tmp = lambda./(4*pi*dist);
  12. if nargin > 2
  13. tmp = tmp*sqrt(Gt);
  14. end
  15. if nargin > 3
  16. tmp = tmp*sqrt(Gr);
  17. end
  18. PL = -20*log10(tmp);

在教材中,还有一个基于这个Matlab代码生成的图表。我的问题是,如何根据这个函数自己创建这个图表?我是否需要为所有变量提供数据来源?如果我无法获取数据来源,比如Excel文件,是否有Matlab内置工具支持创建这个图表?这个图表类似于这样:
(我了解绘图的概念,它在命令中,输入一些函数的数据,但它只会显示函数输出的答案。我无法在网上找到合适的解决方案,所以我来这里寻求帮助)

在Matlab中如何使用函数创建数据可视化。

在修订后的问题中:
在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:

  1. function PL=PL_free(fc,dist,Gt,Gr)
  2. % Free Space Path loss Model
  3. % Input
  4. % fc : carrier frequency[Hz]
  5. % dist : between base station and mobile station[m]
  6. % Gt : transmitter gain
  7. % Gr : receiver gain
  8. % output
  9. % PL : path loss[dB]
  10. lamda = 3e8/fc;
  11. tmp = lamda./(4*pi*dist);
  12. if nargin>2
  13. tmp = tmp*sqrt(Gt);
  14. end
  15. if nargin>3
  16. tmp = tmp*sqrt(Gr);
  17. end
  18. 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)

在Matlab中如何使用函数创建数据可视化。

After revision:
在Matlab中如何使用函数创建数据可视化。

答案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 onhold all,并使用新的PL进行semilogx

英文:

I haven't testet this, but something along the lines of:

  1. fc = __ % Choose value
  2. Gt = __ % Choose value
  3. Gr = __ % Choose value
  4. dist = logspace(0, 3, 16);
  5. PL=PL_free(fc,dist,Gt,Gr);
  6. semilogx(dist, PL, 'ro')

Alternatively:

  1. dist = logspace(0, 3, 16);
  2. PL = zeros(1, numel(dist));
  3. for ii = 1:numel(dist)
  4. PL(ii) = PL_free(fc, dist, Gt, Gr);
  5. end
  6. 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.

huangapple
  • 本文由 发表于 2023年2月6日 07:50:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定