zzm99


  • Home

  • Tags

  • Categories

  • Archives

  • Search

进阶绘图

Posted on 2019-08-27 | In matlab |
Words count in article: 599 | Reading time ≈ 3

Logarithm Plots

当数据出现指数型变化时,如果继续使用polt的线性刻度,则低次幂部分无法完全表现出来,所以需要采用semilogx、semilogy、loglog函数来解决对数数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>> x = logspace(-1,1,100);
>> y = x.^2;
>> subplot(2,2,1);
>> plot(x,y);
>> title('Plot');
>> subplot(2,2,2);
>> semilogx(x,y);
>> title('Semilogx');
>> subplot(2,2,3);
>> semilogy(x,y);
>> title('Semilogy');
>> subplot(2,2,4);
>> loglog(x,y);
>> title('Loglog');
>> set(gca,'XGrid','on');
>>
Read more »

excel-VBA与宏

Posted on 2019-08-26 | In excel |
Words count in article: 233 | Reading time ≈ 1

什么是VBA、什么是宏

VBA( Visual Basic for Application ):是office软件中内嵌的一种应用程序开发工具,其语言结构和开发环境与VB相似。

宏:由一系列的菜单选项和操作指令组成的、用来完成特定任务的指令集合。

VBA与宏的关系

宏是录制出来的程序,VBA是人手工编译的程序,宏录制出来的程序其实就是一系列的VBA语言“宏”像一个机器人,VBA是用来制作宏这个“机器人”的工具和零件

MS-Excel提供的函数或功能可能不足以执行复杂计算的基本内置功能。在这种情况下,VBA就成为解决这些复杂计算的一种最优方案了。

访问VBA编辑器

在Excel窗口中,找到开发工具 -> Visual Basic,或按ALT + F11。

1

编写一个简单的宏

VBA教程

bianchengzhimei4

Posted on 2019-08-26 | In 编程之美 |
Words count in article: 0 | Reading time ≈ 1

一摞烙饼的排序

Posted on 2019-08-26 | In 编程之美 |
Words count in article: 53 | Reading time ≈ 1

假设有n快大小不一的烙饼,那最少要翻几次,才能达到大小有序的结果呢?

(每次翻转,只能抓住最上面的几块,整个整体上下颠倒)

中国象棋将帅问题

Posted on 2019-08-26 | In 编程之美 |
Words count in article: 444 | Reading time ≈ 2

中国象棋将帅问题

在中国象棋里将和帅是不能碰面的,如下图所示,当将位于d10时,帅就不能在d1,、d2、d3。请写一个程序,输出将、帅所有的合法位置。

要求在代码中仅用一个字节存储变量。

Read more »

有关了解当前线程、进程、系统效能的API

Posted on 2019-08-26 | In 编程之美 |
Words count in article: 545 | Reading time ≈ 2

Sleep()—让当前线程停下来

Sleep函数可以使计算机程序(进程,任务或线程)进入休眠,使其在一段时间内处于非活动状态。当函数设定的计时器到期,或者接收到信号、程序发生中断都会导致程序继续执行。

1
2
3
4
在Windows系统中:
Sleep(2*1000); //sleep for 2 seconds
在Unix系统中:
sleep(2); //sleep for 2 seconds

WaitForSingleObject()—自己停下来,等待某个事件发生

GetTickCount()

GetTickCount是一种函数。GetTickCount返回(retrieve)从操作系统启动所经过(elapsed)的毫秒数,它的返回值是DWORD。

常常用来判断某个方法执行的时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//代替time函数来初始化随机数生成器
#include <iostream>
#include <windows.h>
#include <WinBase.h>
#include <ctime>
using namespace std;
int main()
{
int i, k, r;
for (i = 0; i < 10; i++)
{
srand (GetTickCount());
cout<<endl;
for (k = 0; k < 5; k++)
{
r = rand ();
cout<<r<<endl;
}
}
return 0;
}

QueryPerformanceFrequency()、QueryPerformanceCounter()—访问到精度更高的CPU数据

QueryPerformanceFrequency是操作系统的性能统计分辨率,也就是每秒钟统计多少次的意思。 性能统计频率和应用程序性能没有关系。

QueryPerformanceCounter 是系统性能统计计数器,表示统计了多少次,除以QueryPerformanceFrequency,得到系统运行时间(秒数)。

QueryPerformanceCounter2-QueryPerformanceCounter1,得到高精度(微秒级,=1/QueryPerformanceFrequency秒)的时间差,常用于winddows高精度计时。

timeGetSystemTime()—另一个得到高精度时间的方法

timeGetSystemTime,函数返回系统时间,(毫秒)。这里的系统时间是指自从系统启动时到调用该函数时的时间间隔。

PerformanceCounter()—效能计数器

GetProcessorInfo()/SetThreadAffinityMask()—遇到多核的问题时候更好地控制CPU

GetCPUTickCount()—得到CPU核心运行周期数

让CPU占用率曲线听你指挥

  1. CPU占用率固定在50%,为一条直线

  2. CPU占用率为一条直线,具体占用率由命令行参数决定(参数范围1-100)

  3. CPU占用率状态是一个正弦曲线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const DWORD busyTime = 10; //10ms
const DWORD int idleTime = busyTime; //same ratio will lead to 50% cpu usage

Int64 startTime = 0;

while(true)
{
DWORD startTime = GetTickCount();
//busy loop
while((GetTickCount() - startTime) <= busyTime)
;

//idle loop
Sleep(idleTime);
}

数值计算部分概念

Posted on 2019-08-26 | In 数值计算 |
Words count in article: 476 | Reading time ≈ 1

解误差

中文名称:解误差

外文名称:error of solution

正文内容:又称总离散误差。是真解与数值解之间的误差。

1
2
3
X为数值解
Xn为真解
解误差:|X-Xn|

残差

残差在数理统计中是指实际观察值与估计值(拟合值)之间的差。

1
2
例如对于:Ax = b
残差为: |AX1-b|

舍入误差

舍入误差(英语:round-off error),是指运算得到的近似值和精确值之间的差异。 比如当用有限位数的浮点数来表示实数的时候(理论上存在无限位数的浮点数)就会产生舍入误差。

截断误差

由实际问题建立起来的数学模型,在很多情况下要得到准确解是困难的,通常要用数值方法求它的近似解,例如常把无限的计算过程用有限的计算过程代替,这种模型的准确解和由数值方法求出的近似解之间的误差称为截断误差。 因为截断误差是数值计算方法固有的,因此又称方法误差。

超定方程组

超定方程组是指方程个数大于未知量个数的方程组。

欠定方程组

方程个数小于未知量个数的方程组。

恰定方程组

方程数等于未知数个数

LU分解

在线性代数与数值分析中,LU分解是矩阵分解的一种,将一个矩阵分解为一个下三角矩阵和一个上三角矩阵的乘积,有时需要再乘上一个置换矩阵。 LU分解可以被视为高斯消去法的矩阵形式。在数值计算上,LU分解经常被用来 解线性方程组、且在求反矩阵和计算行列式 中都是一个关键的步骤。

基础绘图

Posted on 2019-07-31 | In matlab |
Words count in article: 245 | Reading time ≈ 1

plot from “Data”

plot()

  • plot(x,y) 画每一个对应的点x、y
  • plot(y) 画每一个点(x, y) 其中x=[1,2,3…, n], n = length(y)
  • 例: plot(cos(0:pi/20:2*pi)); 画的是y为一连串cos值,x为0、1、2、…

hold on/off

plot画图会有服盖效果,先画cos 再画sin 那么看到的是sin 而不是sin和cos的重叠

如果不想matlab每次画图都刷新画板,使用hold on

结束画图后使用hold off

例如:

1
2
3
4
hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off

plot style

  • plot(x, y, ‘str’); xy后面的参数指定画出来图形的风格

1

例如:

1
2
3
4
hold on
plot(cos(0:pi/20:2*pi),'or');
plot(sin(0:pi/20:2*pi),'xg');
hold off
  • cos画的是红色圈圈
  • sin画的是绿色叉叉

legend()

  • 用于给图添加注解
  • legend(‘L1’,’L2’…) L1对应为画的第一条线

1

1

title and ?label()

  • title(‘…’)
  • xlabel(‘…’)
  • ylabel(‘…’)
  • zlabel(‘…’)

text() and annotation()

  • Text里面要表示数学公式要使用LaTex标准

1

1

变量与档案存储

Posted on 2019-07-31 | In matlab |
Words count in article: 879 | Reading time ≈ 5

MATLAB Data (Variables) Types

Multidimensional Array:

  • logical
  • char
  • numeric: double(default)、int8、uint8、single、int16、uint16、int32、uint32、int64、uint64
  • cell
  • struct

Scalar

  • function handle(@)

Variable(Data) Type Conversion

像function一样使用

  • double()
  • single()
  • int8()
  • int16()
  • int32()
  • int64()
  • uint8()
  • uint16()
  • uint32()
  • uint64()

Logical Operations and Assignments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
>> s1 = 'sample';
>> s2 = 'abcded';
>> s3 = [s1 s2]

s3 =

'sampleabcded'

>> s4 = [s1; s2]

s4 =

2×6 char 数组

'sample'
'abcded'

>> s3(s3 == 'a') = 'Z'

s3 =

'sZmpleZbcded'

>> s3 == 'e'

ans =

1×12 logical 数组

0 0 0 0 0 1 0 0 0 0 1 0

>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>> s1 = 'a b c d e f g'

s1 =

'a b c d e f g'

>> s1(1)

ans =

'a'

>> length(s1)

ans =

13

>> s2 = s1(length(s1):-1:1)

s2 =

'g f e d c b a'

>> s2 = s1(end:-1:1)

s2 =

'g f e d c b a'

>>

cat()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>> A = [1 2;3 4]; B = [5 6; 7 8];
>> C = cat(1,A,B)

C =

1 2
3 4
5 6
7 8

>> C = cat(2, A, B)

C =

1 2 5 6
3 4 7 8

>> C = cat(3, A, B)

C(:,:,1) =

1 2
3 4


C(:,:,2) =

5 6
7 8

>>

reshape()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
>> A = {'james', [1 2;3 4;5 6]; pi, magic(5)}

A =

2×2 cell 数组

{'james' } {3×2 double}
{[3.1416]} {5×5 double}

>> C = reshape(A, 1, 4)

C =

1×4 cell 数组

列 1 至 3

{'james'} {[3.1416]} {3×2 double}

列 4

{5×5 double}

>> A(4)

ans =

1×1 cell 数组

{5×5 double}

>> A(2)

ans =

1×1 cell 数组

{[3.1416]}

>> A(3)

ans =

1×1 cell 数组

{3×2 double}

M = magic(n) 生成一个n*n的矩阵,矩阵元素是由整数1到n^2组成的并且任何行任何列的和都相等,阶数n必须是大于等于3的标量。

1
2
3
4
5
6
7
8
9
10
11
>> a = magic(5)

a =

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

>>

File Access

File System <--> Work Space

save() and load()

  • Save (all) workspace data to a file:

    1
    2
    3
    4
    clear;
    a = magic(4);
    save mydata1.mat
    save mydata2.mat -ascii
  • Load data stored in a file:

    1
    2
    load('mydata1.mat')
    load('mydata2.mat','-ascii')

Excel File Reading: xlsread()

  • Read from Excel spreadsheet
    1
    2
    Score = xlsread('04Score.xlsx')
    Score = xlsread('04Score.xlsx','B2:D4')

Excel File Writing: xlswrite()

  • Calculate the means and write into Excel spreadsheet

    1
    2
    3
    M = mean(Score')';
    xlswrite('04Score.xlsx', M, 1, 'E2:E4');
    xlswrite('04Score.xlsx', {'Mean'}, 1, 'E1');
  • mean是求列平均数的函数,所以要使用两次“转置”

  • 第二个参数是要写进去的东西
  • 第三个参数是excel的第几页
  • 第四个参数是位置

Getting Text in Excel Spreadsheet

  • Getting both the text and numbers

[Score Header] = xlsread('04Score.xlsx')

Low-level File Input/Output

Low-level File I/O Functions

  • fopen
  • fclose
  • fscanf
  • fprintf
  • feof

Open and close a file:

fid = fopen('[filename]', '[permission]');

status = fclose(fid);

permission: ‘r’,’r+’,’w’,’w+’,’a’,’a+’

Writing Sine(正弦) Values into A File

1
2
3
4
5
6
7
8
x = 0:pi/10:pi;
y = sin(x);
fid = fopen('sinx.txt', 'w');
for i=1:11
fprintf(fid, '%5.3f %8.4f\n', x(i), y(i));
end
fclose(fid);
type sinx.txt

Read and Write through Formatted I/O

  • Read: A = fscanf(fid, format, size);
  • Write: fprintf(fid, format, x, y, …);

Reading from Files

  • Check if it is the end of file: feof(fid)
1
2
3
John 1995 12 5 12.3 3.24
Tom 1995 12 7 2.3 2.0
Jean 1996 3 2 10.2 0
1
2
3
4
5
6
7
8
9
10
11
fid = fopen('asciiData.txt', 'r'); i = 1;
while ~feof(fid)
name(i,:) = fscanf(fid, '%5c', 1);
year(i) = fscanf(fid, '%d', 1);
no1(i) = fscanf(fid, '%d', 1);
no2(i) = fscanf(fid, '%d', 1);
no3(i) = fscanf(fid, '%g', 1);
no4(i) = fscanf(fid, '%g\n');
i = i+1;
end
fclose(fid);
  • name那里的冒号:的意思是对应char数组的每一行

结构化程式与自定函数

Posted on 2019-07-31 | In matlab |
Words count in article: 362 | Reading time ≈ 1

MATLAB Script

  • .m结尾的文件
  • 不需要像c/c++一样需要编译
  • %开头表示注解, %%则表示section一个块,可选择run section来运行一个子块来debug。
  • 设置断点直接点击代码对应那行最左边序号,显示红点
  • ctrl+A或者手动选代码右键有注释与解注释以及智能缩进smart-indent功能

structure programming

1

1

所有块语法都要带上end结尾

if elseif else

“elseif” and “else” are optional

1
2
3
4
5
6
a = 3;
if rem(a,2) == 0 % rem求余
disp('a is even')
else
disp('a is odd')
end

switch

1
2
3
4
5
6
7
8
9
10
11
input_num = 1;
switch input_num
case -1
disp('negative 1');
case 0
disp('zero');
case 0
disp('positive 1');
otherwise
disp('other value');
end

while

1
2
3
4
n = 1;
while prod(1:n) < 1e100 % prod-> product 乘的意思 1:n->[1,2,3,...,n] n! < 1e100 ->1*10^100
n = n+1;
end

for

1
2
3
4
for n = 1:10
a(n) = 2^10; % a(n) 表示a中第n个元素
end
disp(a)

Tips for Script Writing

  • At the beginning of your script, use:
    • clear all to remove previous variables
    • close all to close all figures
  • use ‘;’ at the end of commands unwanted output
  • use ellipsis … to make scripts more readable:

    1
    2
    A = [1 2 3 4 5 6; ...
    6 5 4 3 2 1];
  • Press Ctrl+c to terminate the script before conclusion

Scripts vs Functions

  • Scripts and functions are both .m files that contain MATLAB commands
  • Function are written when we need to perform routines

Content of MATLAB Built-in Functions

edit(which('mean.m'))

1

User Define Functions

1

Functions with Multiple Inputs and Outputs

1

Function Handles

a way to create anonymous functions

one line expression functions that do not have to be defined in .m files

1
2
3
f = @(x) exp(-2*x);
x = 0:0.1:2;
plot(x, f(x));
1…151617…38
zzm99

zzm99

372 posts
40 categories
3 tags
GitHub
0%
© 2020 zzm99 | Site words total count: 409.1k
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4