使用相同数据在matlab和R中绘制不同的图
你好,我用相同的数据写了两个程序。一个在R中,另一个在matlab中,但是当我运行这两个程序时,我在图中得到了不同的结果 R中的代码如下所示使用相同数据在matlab和R中绘制不同的图,r,matlab,R,Matlab,你好,我用相同的数据写了两个程序。一个在R中,另一个在matlab中,但是当我运行这两个程序时,我在图中得到了不同的结果 R中的代码如下所示 c2 = 1660 p1 = 1000 p2 = 1200 f = 500 w = 2 *pi * f k1 = w / c1 k2 = w / c2 th1 = seq( 0,89, 0.5) th1 = th1 * pi / 180 th2 = asin( pmin(pmax((c2 / c1) * sin (th1),-1.0),1.0) ) k
c2 = 1660
p1 = 1000
p2 = 1200
f = 500
w = 2 *pi * f
k1 = w / c1
k2 = w / c2
th1 = seq( 0,89, 0.5)
th1 = th1 * pi / 180
th2 = asin( pmin(pmax((c2 / c1) * sin (th1),-1.0),1.0) )
kz1 = k1 * cos(th1)
kz2 = k2 * cos(th2)
Tp = (2 * kz1 * p1) / (kz1 * p2 + kz2 * p1)
MetroTp=abs(Tp)
th1=th1*180/pi
th1 = seq( 0,89, 0.5)
plot(th1 , MetroTp, type = "l", xlab="Angle of Incidence (Deg)", ylab="|Tp|")
title(main="Transmition Coefficient")
print(MetroTp)
MetroTp打印和绘图的结果如下:
$Rscript main.r
[1] 0.9507446 0.9507481 0.9507585 0.9507760 0.9508005 0.9508320 0.9508706
[8] 0.9509163 0.9509690 0.9510290 0.9510961 0.9511704 0.9512520 0.9513410
[15] 0.9514374 0.9515413 0.9516528 0.9517718 0.9518987 0.9520333 0.9521758
[22] 0.9523264 0.9524852 0.9526521 0.9528275 0.9530114 0.9532040 0.9534053
[29] 0.9536157 0.9538352 0.9540639 0.9543022 0.9545502 0.9548080 0.9550759
[36] 0.9553542 0.9556430 0.9559426 0.9562533 0.9565753 0.9569088 0.9572543
[43] 0.9576120 0.9579823 0.9583654 0.9587617 0.9591716 0.9595955 0.9600339
[50] 0.9604870 0.9609555 0.9614397 0.9619401 0.9624574 0.9629919 0.9635444
[57] 0.9641154 0.9647054 0.9653153 0.9659457 0.9665974 0.9672711 0.9679676
[64] 0.9686878 0.9694326 0.9702030 0.9710000 0.9718246 0.9726780 0.9735614
[71] 0.9744761 0.9754233 0.9764045 0.9774213 0.9784751 0.9795678 0.9807010
[78] 0.9818768 0.9830972 0.9843643 0.9856805 0.9870482 0.9884702 0.9899492
[85] 0.9914883 0.9930908 0.9947601 0.9965001 0.9983148 1.0002086 1.0021863
[92] 1.0042529 1.0064141 1.0086758 1.0110446 1.0135277 1.0161329 1.0188687
[99] 1.0217445 1.0247706 1.0279584 1.0313203 1.0348705 1.0386241 1.0425987
[106] 1.0468134 1.0512900 1.0560530 1.0611301 1.0665530 1.0723579 1.0785865
[113] 1.0852873 1.0925167 1.1003412 1.1088398 1.1181072 1.1282585 1.1394351
[120] 1.1518138 1.1656194 1.1811444 1.1987796 1.2190650 1.2427798 1.2711160
[127] 1.3060537 1.3513290 1.4157978 1.5382863 1.6666667 1.6666667 1.6666667
[134] 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667
[141] 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667
[148] 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667
[155] 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667
[162] 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667
[169] 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667 1.6666667
[176] 1.6666667 1.6666667 1.6666667 1.6666667
现在我在matlab中运行相同的代码
代码如下:
clear all;
clc;
c1=1500;
c2=1660;
p1=1000;
p2=1200;
f=500;
w=2*pi*f;
k1=w/c1;
k2=w/c2;
m=1;
for l=1:0.5:90
th1=(l-1)*pi/180;
th2=asin((((c2/c1)*sin(th1))));
kz1=k1*cos(th1);
kz2=k2*cos(th2);
Tp=(2*kz1*p1)/(kz1*p2+kz2*p1);
yy(m)=abs(Tp)
m=m+1;
end
plot(0:0.5:89,yy,'-')
title('Transmition Coefficient')
xlabel('Angle of Incidence(Deg)'), ylabel('|Tp|')
axis([0,90])
结果和我的图如下
对于yy(m)
情节是
我们可以看到,在matlab中,1.666之后,绘图中的直线开始减小其值,但在R中,1.666之后的直线保持稳定
我应该在R中写些什么来修复matlab中相同的绘图
Columns 1 through 7
0.9507 0.9507 0.9508 0.9508 0.9508 0.9508 0.9509
Columns 8 through 14
0.9509 0.9510 0.9510 0.9511 0.9512 0.9513 0.9513
Columns 15 through 21
0.9514 0.9515 0.9517 0.9518 0.9519 0.9520 0.9522
Columns 22 through 28
0.9523 0.9525 0.9527 0.9528 0.9530 0.9532 0.9534
Columns 29 through 35
0.9536 0.9538 0.9541 0.9543 0.9546 0.9548 0.9551
Columns 36 through 42
0.9554 0.9556 0.9559 0.9563 0.9566 0.9569 0.9573
Columns 43 through 49
0.9576 0.9580 0.9584 0.9588 0.9592 0.9596 0.9600
Columns 50 through 56
0.9605 0.9610 0.9614 0.9619 0.9625 0.9630 0.9635
Columns 57 through 63
0.9641 0.9647 0.9653 0.9659 0.9666 0.9673 0.9680
Columns 64 through 70
0.9687 0.9694 0.9702 0.9710 0.9718 0.9727 0.9736
Columns 71 through 77
0.9745 0.9754 0.9764 0.9774 0.9785 0.9796 0.9807
Columns 78 through 84
0.9819 0.9831 0.9844 0.9857 0.9870 0.9885 0.9899
Columns 85 through 91
0.9915 0.9931 0.9948 0.9965 0.9983 1.0002 1.0022
Columns 92 through 98
1.0043 1.0064 1.0087 1.0110 1.0135 1.0161 1.0189
Columns 99 through 105
1.0217 1.0248 1.0280 1.0313 1.0349 1.0386 1.0426
Columns 106 through 112
1.0468 1.0513 1.0561 1.0611 1.0666 1.0724 1.0786
Columns 113 through 119
1.0853 1.0925 1.1003 1.1088 1.1181 1.1283 1.1394
Columns 120 through 126
1.1518 1.1656 1.1811 1.1988 1.2191 1.2428 1.2711
Columns 127 through 133
1.3061 1.3513 1.4158 1.5383 1.6511 1.6292 1.6069
Columns 134 through 140
1.5841 1.5609 1.5371 1.5129 1.4882 1.4631 1.4374
Columns 141 through 147
1.4112 1.3845 1.3574 1.3297 1.3015 1.2728 1.2436
Columns 148 through 154
1.2139 1.1837 1.1529 1.1217 1.0900 1.0578 1.0251
Columns 155 through 161
0.9919 0.9582 0.9240 0.8894 0.8544 0.8189 0.7830
Columns 162 through 168
0.7466 0.7099 0.6727 0.6352 0.5974 0.5592 0.5206
Columns 169 through 175
0.4818 0.4427 0.4033 0.3637 0.3238 0.2838 0.2436
Columns 176 through 179
0.2032 0.1627 0.1221 0.0815