搜索此博客

2010年8月26日星期四

循环 矩阵


for m=1:5
mzero{m}=zeros(2,3);
end 

2010年8月23日星期一

[原创]matlab 数据处理 新得体会

本来ns2是主要奋斗目标,结果最近总是用matlab来处理数据。

线说一下awk,不知道是不是不会用,matlab跑20s的东西,那里要处理3个小时...

Oh My God...转会matlab 其实matlab还是挺强大的!

首先,matlab只要循环就很缓慢,所以一定要熟悉向量操作,.*啊 ./啊 之类的 包括比如给一个index向量,本来是[1 2 3 4... N]以前第一想法就是循环! 千万别

有一个a=1:1:10000; 非常迅速

然后如果实在需要循环,也就是我最近苦恼的问题,实在必须循环的话,就要多用tic toc,看看数值上到底都用了多少秒解决的,然后就着一个比较reasonable的来弄。


比如:
我是time series画图,所以每个时间点的数值都要计算,必须只能是循环...那怎么办呢?而且循环到i很大一次很费时间。所以我们把后面的分段,每次都还是循环那个一点点 就好了。

2010年8月22日星期日

matlab文件读写 读写数据类型:矩阵 ascii

俗话说的好使用要配套!

ascii的好处就是直接打开就能看 不象binary二进制数字乱码



首先读数据: load('filename.ext');

写数据用save,我们喜欢用可以filename是个变量名儿的那种,help save里面有一条这样写着

save(savefilename,'data');

经过尝试,如下可以写入ascii的格式,例

A=[1 2 3 ; 4 5 6; 7 8 9];
filename=['hello1-' num2str(123) '.txt'];

save(filename, 'A', '-ascii');

这样呢就是把矩阵A写入到文件名为 hello1-123.txt里面

matlab 读写文件 文件名里面有读写的办法

因为文件名里面有变量,所以我们利用matlab里面最为经典的中括号原理

比如
a=1;
b=2;
c=[1 2];
如果1 和2 是str,那么c就是两个str的合并了!
数字与字母的转换为 num2str这个函数。

目前为止,还是尽量使用数字,因为一个是字母没法做循环,还有一个是字母非常的造成大型程序很麻烦。

下面是简单的实现,ref某学长,感谢DP学长的耐心指导!


%首先,使用fopen打开一个文件
%文件名是你给该文件的命名,a是mode,详细查matlab help

fid=fopen('filename.txt','a');

% 把需要输入的数据inputData打印在fid实例,也就是filename.txt里面
fprint(fid,'%f\n',inputData);

fclose(fid); % close filename.txt

好,如何实现文件名称变量化呢?我们需要把'filename.txt'使用一个变量来代替,比如filename='filename.txt'

然后filename这个字符串使用[]中括号方式,把字符串在一起,这个串联的过程,就可以出现数字,记得使用数字时候,不要忘记num2str.

好了,现在可以对文件名进行变量化了。这种方法还可以实现任何想要变量化的字符呢!原理你当然是知道的了!

超级啰嗦的温馨提示:一定要注意哦 []的用法是前后要空格,所以变量一样要空格

如:如果想要命名文件名为05-1.txt

那么例如i是循环系数,可是取值1,2,3, 某string变量ss=['05-' num2str(i) '.tr']

千万不要忘了num2tr(i)前后是有空格的呦 要不然汇报错!

OK 完毕!




ref:某学长 嘻嘻 人很好的!嘻嘻^^

2010年8月18日星期三

图书馆里面的书 - maybe useful

Introduction to design and analysis of experiments (QA279 COB (ORDINARY)

Basics of structural equation modeling (QA280 MAR (ORDINARY)



Variance components 
QA279 SEA (ORDINARY)


Confidence intervals on variance components  QA276.74 BUR (ORDINARY)


Estimation of variance components and applications  QA279 RAO (ORDINARY)


The analysis of variance  G70.3 CAT/30 (ORDINARY)     H61 QUA/12 (ORDINARY)    H61 QUA/1 (ORDINARY)


Handbook of statistics / Vol. 1, Analysis of variance   QA276 HAN/1 (ORDINARY)


Applied statistics, analysis of variance and regression  QA279 DUN (ORDINARY)


Experimental design and analysis  QA279 LEE (ORDINARY)




The analysis of variance : a basic course   QA279 HUI (ORDINARY)











matlab load&save


% Load the file to the 
% matrix, M :
 M = load('sample_file.txt') 
 

% Add 5 to M :
M = M +5 


% Save M to a .mat 
% file called 
%'sample_file_plus5.mat':
save sample_file_plus5 M


% Save M to 
% an ASCII .txt file 
% called 'sample_file_plus5.txt' :
save sample_file_plus5.txt M -ascii 

2010年8月16日星期一

对数函数公式表

logaMN=logaM+logaN
logaM/logaN=logaM-logaN
logaM^n=nlogaM
logbN=logaNb/logab
logaB乘logbA=1
logaB*logbC*logcD=logaD
loga(m)b(n)=n/mlogaB

2010年8月11日星期三

MatLab * / .* ./

Need to notice that

.*  : Array multiply
*   : Matrix multiply

./  : Right array divide  
/   : Slash or right matrix divide

Example:

coef = zeros(1,1001);
 coef(1) = y(1);
 y = (y(2:1001)-y(1:1000))./(x(2:1001)-x(1:1000));



ref:
http://www.cyclismo.org/tutorial/matlab/operations.html

matlab 加速

The ability to work with these vector functions is one of the advantages of Matlab. Now complex operations can be defined that can be done quickly and easily. In the following example a very large vector is defined and can be easily manipulated. (Notice that the second command has a ";" at the end of the line. This tells Matlab that it should not print out the result.)
>> x = [0:0.1:100]

x =

  Columns 1 through 7 

         0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000

    [stuff deleted]

  Columns 995 through 1001 

   99.4000   99.5000   99.6000   99.7000   99.8000   99.9000  100.0000


>> y = sin(x).*x./(1+cos(x));
ref:
http://www.cyclismo.org/tutorial/matlab/operations.html

vector operation in MatLab - how to read portions of a vector

v = [0:2:8]

v =

     0     2     4     6     8





>> v(1:3)

ans =

     0     2     4

>> v(1:2:4)

ans =

     0     4

ref: http://www.cyclismo.org/tutorial/matlab/vector.html

2010年8月6日星期五

mac os 显示隐藏 文件 文件夹

显示:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏:defaults write com.apple.finder AppleShowAllFiles -bool false

2010年8月5日星期四

mac os 使finder在标题栏上显示完整目录

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
killall Finder















Reference:http://mac.linsheng.me/archives/515.html

Matlab常用画图语句

1. 画图
(1)普通画图:
plot(x-axis,y-axis,'color&style');
(2)横纵都为log图:
loglog(x-axis,y-axis,'color&style');
(3)横正常;纵log图:
semilogy(k09,pM09,'-r',k09,pT09,'^r',k07,pM07,'-b',k07,pT07,'^b',...
k05,pM05,'-k',k05,pT05,'^k',k03,pM03,'-m',k03,pT03,'^m');

2. 给图加注释
xlabel('what is on x-axis');
ylabel('what is on y-axis');
legend('1st line - name','2nd line - name','',...);

titlestr = 'title of this figure';
%图的标题有变量的时候
titlestr= ['M/M/1 steady state (\rho=',num2str(rho),')'];
title(titlestr);

hold on;
grid on;
set(gca,'YTick',[1e-12 1e-11 1e-10 1e-9 1e-8 1e-7 1e-6 1e-5 1e-4 1e-3 1e-2 1e-1 1e0]);

2010年7月28日星期三

pdf 概率密度函数(probability density function)

概率密度函数(Probability Density Function, PDF)为描述随机变量的概率分布。亦为累积分布函数(CDF)之导函数。

Probability distributions
The set of probabilities associated with all possible (mutually exclusive) events is called a probability function,
a MASS FUNCTION if the events are discrete,
a DENSITY FUNCTION if continuous.
The sum of this set of probabilities is always 1.

e.g.
If the events are discrete values, e.g. 1, 2, 3 etc, then the distribution is called a discrete distribution:
Σ P(i)= i
1

If the events can be any value in a continuum, then the distribution is called continuous and:
∫ f(t)=1
t=0

Note that f(t) is NOT the probability of t.

Change Margin - Mac Office 2008

Way to change margin of mac office 2008:

GO TO Format Tab -> Document option -> you will get the margin set up..

2010年7月26日星期一

[zz] MathType5.2和MS office 2007兼容性问题解决办法

写报告,里面有公式是非常必不可少的功课,之前都是使用mathType,非常好用美观~ 不过不太支持2007Word,这个办法就可以了~

Here is some infromation from support at Design Science that may help with your issue:

This message addresses questions concerning MathType for Windows version 5.2c and compatibility with the recently released Microsoft Word 2007. The scope of this message addresses only Microsoft Word 2007. We are currently testing other Office 2007 applications and information for those is not yet available.

We are currently in development of a new release of MathType which addresses compatibility with Microsoft Office 2007. That release is forthcoming and appropriate announcements will be made upon its completion.

In the interim we offer a manual set-up to use MathType 5.2c with Microsoft Word 2007. While we have thoroughly tested MathType 5.2c with Word 2007, please note that this is a work-around, thus you should not consider this a final solution.

For now you can do the following:

1. Copy the MathType Commands file to the Microsoft Word Startup Folder:

a. Locate the file, MathType Commands 5 for Word.dot. If you installed MathType to its default location (c:\Program files) this file will be found at: C:\Program Files\MathType\Office Support\ If you installed MathType to another location this path must be adjusted accordingly.

b. Locate the file, MathType Commands 5 for Word.dot and right-click it and choose Copy.

c. Navigate to C:\Program Files\Microsoft Office\Office12\Startup

d. In this Startup Folder, right-click and choose Paste. This will copy the MathType Commands 5 for Word.dot into the Startup folder.

2. Copy the MathPage.wll file to the Microsoft Word Startup Folder:

a. Locate the file MathPage.wll. If you installed MathType to its default location (c:\Program files) this file will be found at:

C:\Program Files\MathType\MathPage\ If you installed MathType to another location this path must be adjusted accordingly.

b. Locate the file, MathPage.wll and right-click it and choose Copy.

c. Navigate to C:\Program Files\Microsoft Office\Office12\Startup

d. In this Startup Folder, right-click and choose Paste. This will copy the MathPage.wll into the Startup folder.

Addressing Trust (Security) settings:

If your trust settings are such that copying the above mentioned files to C:\Program Files\Microsoft Office\Office12\Startup still results in MathType not working and a message appears saying that Macros are disabled, these files must be copied instead to a different location as follows:

C:\Documents and Settings\\Application Support\Microsoft\Word\Startup

If by custom installation your trusted startup folder is in a different location, Microsoft Word 2007 can help you find it. Here’s how to locate the trusted startup folder location using Word.

1. Launch Word 2007

2. From the Office Button drop-down menu choose Word Options

3. In the resulting window choose Resources from the left, and from the right press the About button

4. In the next window press the System Info Button

5. In the resulting window click to expand Microsoft Office 2007 Applications

6. Then click to expand Microsoft Word 2007

7. Locate and click once on Startup

8. The pane at right will indicate the path for the currently installed Trusted Word Startup folder.

Simply follow the procedures mentioned above but copy both the MathType Commands 5 for Word.dot and MathPage.wll files into this location instead. This is considered a “Trusted location” and will allow the macros to run when you launch Microsoft Word.

When you start Word 2007, you will see a new Tab called Add-ins Click this tab to reveal the MathType toolbar and the MathType Menu.

Should you discover any problems or experience any odd behaviors using MathType 5.2c with Word 2007, please let us know by emailing us at support(at)dessci.com

Hope this helps.

Tech Support

[原创] ns2-2.33在苹果系统 MAC OS X 10.6.3上的安装

一直使用windows系统 + cygwin + ns2 2.34

最近入手Macbook pro一小台,本来试图安装win7,然后继续的,结果屡次不成功,某学术男提示说,你为什么不在mac系统上安装呢,那个也是unix系列的系统。于是便开始了尝试,一共两天吧,最终算是成功了,现在写下安装过程~

Step1. 下载xcode (http://developer.apple.com/technologies/tools/xcode.html) 直接安装就可以,会安装到一个叫做/Developer的文件夹下面,然后寻找/Developer/SDKs/MacOSX10.6.sdk/X11R6/lib目录下有很多文件,就是后来要用的。(reference:http://lovefei.com/?p=86)

Step2. 下载ns2 2.33 allinone package (http://sourceforge.net/projects/nsnam/files/),进入mac的terminal使用tar zxvf ns-allinone-2.33.tar.gz 来解压缩。

Step3. 最关键的一步,需要手动修改ns-allinone-2.33目录下面的install文件,右键(control+单击)打开方式选择xcode, 然后修改如下:(reference:http://old.nabble.com/Installation-on-OSX-10.6-td27790838.html)

#! /bin/sh
#
# Copyright (C) 2000 by USC/ISI
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation, advertising
# materials, and other materials related to such distribution and use
# acknowledge that the software was developed by the University of
# Southern California, Information Sciences Institute. The name of the
# University may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# $Header: /cvsroot/nsnam/ns-2/allinone/install,v 1.31 2007/03/10 23:40:05 tom_henderson Exp $

X_INCL = /Developer/SDKs/MacOSX10.6.sdk/usr/X11R6/include
X_LIBS = /Developer/SDKs/MacOSX10.6.sdk/usr/X11R6/lib
MACOS_CONF = "--x-include=$X_INCL --x-libraries=$X_LIBS"

die() {
echo "$@" 1>&2
test ! -z "$blame" && echo "$blame" 1>&2
exit 1
}

warn() {
echo "$@"
}

。。。

。。。

# Build otcl

echo "============================================================"
echo "* Build OTcl-$OTCLVER"
echo "============================================================"

cd ./otcl-$OTCLVER

blame='Please check http://www.isi.edu/nsnam/ns/ns-problems.html
for common problems and bug fixes.'
if [ "${test_cygwin}" = "true" ]; then
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include || die "otcl-$OTCLVER configuration failed! Exiting ...";
else
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include CFLAGS="-framework CoreFoundation" || die "otcl-$OTCLVER configuration failed! Exiting ...";
fi

if make
then
echo "otcl-$OTCLVER has been installed successfully."
else
echo "otcl-$OTCLVER make failed! Exiting ..."
echo "See http://www.isi.edu/nsnam/ns/ns-problems.html for problems"
exit
fi

cd ..

# Build tclcl

echo "============================================================"
echo "* Build Tclcl-$TCLCLVER"
echo "============================================================"

cd ./tclcl-$TCLCLVER

if [ "${test_cygwin}" = "true" ]; then
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include || die "tclcl-$TCLCLVER configuration failed! Exiting ...";
else
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include --with-otcl=../otcl-$OTCLVER || die "tclcl-$TCLCLVER configuration failed! Exiting ..."
fi

if make
then
echo "tclcl-$TCLCLVER has been installed successfully."
else
echo "tclcl-$TCLCLVER make failed! Exiting ..."
echo "See http://www.isi.edu/nsnam/ns/ns-problems.html for problems"
exit
fi

cd ../

# John's hack
test -f ./otcl-$OTCLVER/libotcl.a && rm ./otcl-$OTCLVER/libotcl.so

echo "============================================================"
echo "* Build ns-$NSVER"
echo "============================================================"

cd ./ns-$NSVER
if [ -f Makefile ] ; then
make distclean
fi

if [ "${test_cygwin}" = "true" ]; then
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include || die "Ns configuration failed! Exiting ...";
else
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include LIBS="-framework CoreFoundation" --with-otcl=../otcl-$OTCLVER --with-tclcl=../tclcl-$TCLCLVER || die "Ns configuration failed! Exiting ...";
fi

if make
then
echo " Ns has been installed successfully."
else
echo "Ns make failed!"
echo "See http://www.isi.edu/nsnam/ns/ns-problems.html for problems"
exit
fi

cd ../

# Build nam

echo "============================================================"
echo "* Build nam-$NAMVER"
echo "============================================================"

ln -s otcl-$OTCLVER otcl
ln -s tclcl-$TCLCLVER tclcl

cd ./nam-$NAMVER

# XXX temporary OS X hack
if [ "${test_darwin}" = "true" ]; then
ln -s /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation libcorefoundation.dylib
fi

if [ "${test_cygwin}" = "true" ]; then
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include --with-tclcl=$CUR_PATH/tclcl-$TCLCLVER || die "Nam configuration failed! Exiting ...";
else
./configure --x-libraries=/usr/X11R6/lib --x-includes=/usr/X11R6/include V_LIBS="-framework CoreFoundation" --with-otcl=../otcl-$OTCLVER --with-tclcl=../tclcl-$TCLCLVER || die "Nam configuration failed! Exiting ...";
fi

Step4. 之后cd ns-allinone-2.33运行./install即可。

Step5. 修改环境变量

1)echo "PATH=/Users/username/ns2/ns-allinone-2.31/bin:$PATH" > .bashrc (reference:http://sites.google.com/site/pengjungwu/install-ns2-31-on-osx)

2)(http://lovefei.com/?p=86) 5.1 input “echo $SHELL” and you can see if it is “bash“, which means Bourne Shell. (os X after 10.3 is using Bourne Shell by default)


5.2 modify .profile or .bash_profile at home, add the related variables like this:
export PATH=$PATH:/Users/wangxiaofei/work/ns/229/bin:/Users/wangxiaofei/work/ns229/tcl8.4.11/unix:/Users/wangxiaofei/work/ns/229/tk8.4.11/unix

export LD_LIBRARY_PATH=/Users/wangxiaofei/work/ns/229/otcl-1.11:/Users/wangxiaofei/work/ns/229/lib

export TCL_LIBRARY=/Users/wangxiaofei/work/ns/229/tcl8.4.11/library
5.3 input “source .profile“, you can run ns2 well now!~~~ ^^

[zz] 如何产生真随机数—— 针对每次都是伪随机~

NS-2随机数产生器:

1.随机数产生器所产生的数值是由种子和分布所控制的,不同的种子或者是分布就会产生不同的随机数。当一个种子和分布决定之后,随机数产生器会产生一出一个由一长串不同数字所组成的表格,当需要一个随机数时,随机数产生器就会去选取这个表格中的一个数字,当需要另一个随机数时,随机数产生器就会去选取这个表格中第二个数字,依此类推。所以当使用种子和分布相同时,得到的随机数就会相同;若不同时,得到的随机数就会不同。在NS2中,若种子的值是0,表示每次执行程序的时候,随机数产生器都会产生出不同的表格,也就是说每次的得到随机数都不相同。
2.设置种子
若要在NS2中产生一个随机数产生器,并把种子设置为1,则可以把下的程序代码放入tcl code中。
set rng [new RNG]
$rng seed 1
3.设置分布
在NS2中所提供的分布有Pareto,Constant,Uniform,Exponential或HyperExponentail等。
(1)Pareto Distribution: 要提供expectation (avg_)和shaper parameter (shape_)参数,实例如下:
set r1 [new RandomVariable/Pareto]
$r1 use-rng $rng
$r1 set avg_ 10.0
$r1 set shape_ 1.2
(2)Constant:要提供平均值(avg_)参数,实例如下所示.
set r2 [new RandomVariable/Constant]
$r2 use-rng $rng
$r2 set avg_ 5.0
(3)Uniform Distribution :要提供最小值(min_)和最大值(max_)参数,实例如下所示:
set r3 [new RandomVariable/Uniform]
$r3 use-rng $rng
$r3 set min_ 0.0
$r3 set max_ 10.0
(4)Exponential Distribution:要提供平均值(avg_)参数,实例如下所示:
set r4 [new RandomVariable/Exponential]
$r4 use-rng $rng
$r4 set avg_ 5
(5)Hyperexponential Distribution:要提供平均值(avg_)和cov_参数,实例如下所示:
set r5 [new RandomVariable/HyperExponential]
$r5 use-rng $rng
$r5 set avg_ 1.0
$r5 set cov_ 4.0

测试随机数产生器:
set rng [new RNG]
$rng seed 1

puts "Testing Pareto Distribution"
set r1 [new RandomVariable/Pareto]
$r1 use-rng $rng
$r1 set avg_ 10.0
$r1 set shape_ 1.2
for {set i 1} {$i<=3} {incr i} { puts [$r1 value] } puts "Testing Constant Distribution" set r2 [new RandomVariable/Constant] $r2 use-rng $rng $r2 set avg_ 5.0 for {set i 1} {$i<=3} {incr i} { puts [$r2 value] } puts "Testing Uniform Distribution" set r3 [new RandomVariable/Uniform] $r3 use-rng $rng $r3 set min_ 0.0 $r3 set max_ 10.0 for {set i 1} {$i<=3} {incr i} { puts [$r3 value] } puts "Testing Exponential Distribution" set r4 [new RandomVariable/Exponential] $r4 use-rng $rng $r4 set avg_ 5 for {set i 1} {$i<=3} {incr i} { puts [$r4 value] } puts "Testing HyperExponential Distribution" set r5 [new RandomVariable/HyperExponential] $r5 use-rng $rng $r5 set avg_ 1.0 $r5 set cov_ 4.0 for {set i 1} {$i<=3} {incr i} { puts [$r5 value] } 一个在网络仿真中应用到随机数产生器的完整实例: 1.仿真的网络结构图 r1和r2是两个路由器,其中的链路是采用first in first out(FIFO)的队列管理机制,频宽是10Mbps,传递的延迟时间是10ms。来源节点s1、s2、s3和目的节点d1、d2、d3之间有三条FTP数据流,而这三条FTP数据流的起始时间是由随机数所产生的,但是时间都是1-3s,且每条数据流都会传送5s。 2.效果评比指标:吞吐量(Throughput) 定义:单位时间内,所有目的结点的平均接收数据速率 Throughput=(某段时间内所有目的结点的数据接收量)/(统计的这段时间) 另外要补充一点的,在论文的实验中,在计算数据接收量并不是从模拟的起始时间就开始统计,而是让模拟经过一段时间,进入稳态才会开始统计。 3.TCL程序代码(lab4_2.tcl) set ns [new Simulator] #打开一个trace file,用来记录封装包传送过程 set nd [open out.tr w] $ns trace-all $nd #打开一个NAM记录文件 set nf [open out.nam w] $ns namtrace-all $nf #设置TCP flow的数目 set nflow 3 #设置路由器 set r1 [$ns node] set r2 [$ns node] $ns duplex-link $r1 $r2 1Mb 10ms DropTail #设置queue limit为10个packet $ns queue-limit $r1 $r2 10 #设置TCP的来源节点和目的节点 #建立来源和目的节点与路由器的链路 for { set i 1 } { $i<=$nflow } { incr i } { set s($i) [$ns node] set d($i) [$ns node] $ns duplex-link $s($i) $r1 10Mb 1ms DropTail $ns duplex-link $r2 $d($i) 10Mb 1ms DropTail } #建立TCP的联机,并在TCP联机上建立FTP应用程序 for { set i 1 } { $i<=$nflow } { incr i } { set tcp($i) [new Agent/TCP] set sink($i) [new Agent/TCPSink] $ns attach-agent $s($i) $tcp($i) $ns attach-agent $d($i) $sink($i) $ns connect $tcp($i) $sink($i) set ftp($i) [new Application/FTP] $ftp($i) attach-agent $tcp($i) $ftp($i) set type_ FTP } set rng [new RNG] $rng seed 1 set RVstart [new RandomVariable/Uniform] $RVstart set min_ 0 $RVstart set max_ 1 $RVstart use-rng $rng #由随机数产生器去决定每一条flow的起始时间(在0~1s之内) #每条flow传输5s,并在指定的时间,让ftp开始传输和结束 for {set i 1} {$i<=$nflow} {incr i} { set startT($i) [expr [$RVstart value]] puts "startT($i) $startT($i) sec" set endT($i) [expr ($startT($i)+5)] puts "endT($i) $endT($i) sec" $ns at $startT($i) "$ftp($i) start" $ns at $endT($i) "$ftp($i) stop" } proc finish {} { global ns nd nf close $nd close $nf $ns flush-trace exec nam out.nam & exit 0 } #在第7秒时去调用finish函数来结束模拟 $ns at 7.0 "finish" #执行模拟 $ns run 4.分析awk程序代码(4_2.awk) BEGIN{ init=0; startT=0; endT=0; } { action=$1; time=$2; from=$3; to=$4; type=$5; pktsize=$6; flow_id=$8; node_1_address=$9; node_2_address=$10; seq_no=$11; packet_id=$12; #记录类型是tcp 动作是dequeue 发生事件的时间介于1.0-5.0 #由于新增结点时,结点建立的顺序为r1 r2 s1 d1 s2 d2 s3 d3 所以相对的 #结点id就为0 1 2 3 4 5 6 7 if(action=="r"&&type=="tcp"&&time>=1.0&&time<=5.0&&((from==1&&to==3)||(from==1&&to==5) || (from==1&&to==7))) {
if(init==0) {
starT=time;
init=1;
}
#记录在这段时间中离开队列的封包大小总和(in bytes)
pkt_byte_sum+=pktsize;
endT=time;
}

}

END {
#计算1.0-5.0s的平均带宽
printf("startT:%f endT:%f \n",startT,endT);
printf("pkt_byte_sum:%d \n",pkt_byte_sum);
time=endT-startT;
throughput=pkt_byte_sum*8/time/1000000;
printf("throughput:%.3f Mbps \n",throughput);
}


5.执行方法:
$ns lab4_2.tcl
$awk -f 4_2.awk out.tr


6.执行结果:
startT:0.000000 endT:4.993908
pkt_byte_sum:491960
throughput:0.788 Mbps