AnyMath 文档
Notebook

在AnyMath环境中使用MATLAB代码

这个例子演示了如何在AnyMath中调用MATLAB函数,以及它们的应用和与环境函数的比较。

工程中的建筑曲线:

启用显示图形的后端方法:

In [ ]:
Pkg.add(["Statistics"])
   Resolving package versions...
    Updating `~/.project/Project.toml`
  [10745b16] + Statistics v1.11.1
    Updating `~/.project/Manifest.toml`
  [10745b16] + Statistics v1.11.1
  [56f22d72] + Artifacts v1.11.0
  [8f399da3] + Libdl v1.11.0
  [37e2e46d] + LinearAlgebra v1.12.0
  [e66e0078] + CompilerSupportLibraries_jll v1.3.0+1
  [4536629a] + OpenBLAS_jll v0.3.29+0
  [8e850b90] + libblastrampoline_jll v5.15.0+0
In [ ]:
using Plots

我们产生两个信号:

In [ ]:
A = sin.(range(0, 2pi, length=100)')' # 正弦波
B = @. sin(A) + 0.1 * randn() # 噪声正弦波
C = 1:1:100;

我们在图表上显示两个信号:

In [ ]:
plot(C,A)
plot!(C,B)
Out[0]:

我们通过首先连接统计函数库来计算两个信号之间的相关性。:

In [ ]:
using Statistics
engee_cor = cor(A,B)
Out[0]:
1×1 Matrix{Float64}:
 0.9872427316902408

从AnyMath调用MATLAB代码

从Julia,您可以调用任何命令或函数MATLAB,这将返回结果。

连接MATLAB接口:

In [ ]:
using MATLAB

将AnyMath的信号数据加载到MATLAB内核中:

In [ ]:
D = hcat(A,B)
A = D[:,1];
B = D[:,2];
mat"""
A = $A;
B = $B;
R = corrcoef(A,B);
"""

使用MATLAB内核函数计算两个信号之间的相关性:

In [ ]:
matlab_cor = mat"R; R()"
Out[0]:
2×2 Matrix{Float64}:
 1.0       0.987243
 0.987243  1.0

结果的比较和使用MATLAB和AnyMath方法的绝对误差的计算:

In [ ]:
difference = abs(engee_cor[1,1] - matlab_cor[1,2])
println("AnyMath相关性: ", engee_cor[1,1], '\n', "Matlab相关性: ", matlab_cor[1,2], '\n', "绝对误差: ", difference)
Корреляция Engee: 0.9872427316902408
Корреляция Matlab: 0.9872427316902401
Абсолютная погрешность: 6.661338147750939e-16

从MATLAB中调用频率增加的正弦曲线:

In [ ]:
chirp = mat"chirp = dsp.Chirp('InitialFrequency', 0,'SamplesPerFrame', 500); chirp()"
plot(chirp)
Out[0]:

建造一个神奇的广场:

In [ ]:
mat"magic(3)"
Out[0]:
3×3 Matrix{Float64}:
 8.0  1.0  6.0
 3.0  5.0  7.0
 4.0  9.0  2.0

最简单的算术运算:

In [ ]:
x = range(-10.0, stop=10.0, length=50)
y = range(2.0, stop=3.0, length=50)

mat"""
    $u = $x + $y
    $v = $x - $y
"""

plot(u,v)
>> >> >> 
matlab_jl_2 =

   -8.0000
   -7.5714
   -7.1429
   -6.7143
   -6.2857
   -5.8571
   -5.4286
   -5.0000
   -4.5714
   -4.1429
   -3.7143
   -3.2857
   -2.8571
   -2.4286
   -2.0000
   -1.5714
   -1.1429
   -0.7143
   -0.2857
    0.1429
    0.5714
    1.0000
    1.4286
    1.8571
    2.2857
    2.7143
    3.1429
    3.5714
    4.0000
    4.4286
    4.8571
    5.2857
    5.7143
    6.1429
    6.5714
    7.0000
    7.4286
    7.8571
    8.2857
    8.7143
    9.1429
    9.5714
   10.0000
   10.4286
   10.8571
   11.2857
   11.7143
   12.1429
   12.5714
   13.0000
Out[0]:

从AnyMath调用MATLAB文件

在文件管理器中打开的文件夹的路径:

In [ ]:
mat"cd($(@__DIR__))"

启动m脚本:

image.png
In [ ]:
mat"file"
>> >> >> a = 10
In [ ]:
mat"run('file.m')"
>> >> >> a = 10
In [ ]:
mat"""run("file.m")"""
>> >> >> a = 10

启动MATLAB-函数:

image.png
In [ ]:
mat"fun(10,9)"
Out[0]:
103.11111111111111
In [ ]:
x = 12;
y = 13.12;

z = mat"fun($x,$y)"

z/10
Out[0]:
17.0

查看工作区MATLAB:

In [ ]:
mat"whos"
>> >> >>   Name                     Size            Bytes  Class        Attributes

  A                      100x1               800  double                 
  B                      100x1               800  double                 
  R                        2x2                32  double                 
  a                        1x1                 8  double                 
  chirp                    1x1                 8  dsp.Chirp              
  matlab_jl_has_ans        1x1                 8  double                 

清理工作区MATLAB:

In [ ]:
mat"clear"
mat"whos"
>> >> >>   Name                   Size            Bytes  Class     Attributes

  matlab_jl_has_ans      1x1                 8  double              

结论:

这个例子演示了MATLAB函数在AnyMath环境中的使用,它们的联合使用,以及所用方法之间的数值差异。