当前位置:网站首页>08. Use of octave language - control statements, drawing lines and other commands
08. Use of octave language - control statements, drawing lines and other commands
2022-07-22 14:12:00 【WuJiaYFN】
One 、 Control statement
1.1 if … else sentence
Basic grammar :
if expression statements elseif expression statements else statements end
Octave There is no need to use parentheses in the condition () Cover up ( Of course, using parentheses will make the program more explicit
了 ), and statments The program block does not need curly braces *{ }* Cover up .
Octave We need to use end Command to identify if Of the statement end .. stay Octave in ,if…end and if…endif It's all legal . Use end It can make Octave Documents and Matlab file compatible
image if Such control statements , Usually used in Octave In the script , It can also be used on the command line
Octave Indentation has no effect
>> a=1;b=2; >> if a>b c=5 else c=6 end c = 6 >>
1.2 switch sentence
If multiple if/elseif Statement to select from multiple options , Use switch Better statement
The sentence format is :
switch x case x1 statements case x2 statements otherwise statements end
- stay switch In the sentence ,x The value of is in sequence with case Comparison of values in , If you find an equal value , Corresponding statements Will be carried out .
- Notice in Octave in switch The statement is not like C++ That needs to be break sentence ——Octave Found matching options in , perform statements And exit swith sentence . If there is no matching item ,otherwise The statements in are executed .
>> a=1; >> switch a case 0 disp('a is zero') case 1 disp('a is one') otherwise disp('a is not a binary digit') end a is one >>
1.3 for sentence
stay Octave You should use more Use the calculation of vectors instead of for loop , Because usually for The cycle will be much slower . But sometimes for Circulation is inevitable .
for The syntax of circular statements :
for variable=vector statements end
- among vector Contains traversal values . Usually , The vector is expressed with a colon Write
>> for n=1:5 sum=0; sum=sum+n; end >> disp(sum) 5 >>
- Be careful : To at for Adding a semicolon to the loop can avoid octave Output each time the loop body is executed sum Value
1.4 while sentence
If you don't know how many cycles you need to execute , Instead, it knows to end the cycle when a condition is met .Octave Medium while Statement can be realized Now this function
while The basic format of the statement
while expression statements end
Two 、 Simple drawing
Octave By calling another open source software GNUPLOT To achieve very rich drawing functions
The most basic drawing command is
plot(x,y)
, among x,y They are horizontal and vertical axis data respectivelyUse plot() After that, the program will open a new window to display the picture
2.1 plot() Function details
stay Octave You can be in plot Add more options to the command to specify the style of graphic lines
for example , Use red and circles to draw the previous picture , Input
plot(angles,y,'ro');
The last parameter is the string that specifies the line style
adopt title,xlabel and ylabel Name the picture and add a title and xy Axis name :
title('Graph of y=sin(x)') ;
xlabel('Angle');
ylabel('Value')
grid on
Command to add gridlines to the picture :plot Color and style options in the command ( Can also pass help plot see )
2.2 Draw multiple curves
Multiple curves can be drawn on a graph . By means of plot The... Of each curve is given in order in the command x and y vector
legend ()
The command adds the corresponding legend to the pictureBy using hold Command to implement . Use this command , We can realize the presentation on the same picture by two ( Multiple ) plot Command line drawn
hold on
, Continue to draw lines on the graphhold off
, This function will be turned off .
2.3 Draw multiple images
Multiple pictures can be passed figure Command to control , Enter... On the command line
figure
So next plot The command will be drawn in the newly created window ,If you want to return to the previous picture , Input
figure(1)
2.4 Save and print pictures
Octave There is no mouse or shortcut key operation to realize the printing operation of pictures .
Octave Provides print command To print pictures to the default printer .help print The command gives all print The options provided by the command
print('graph1.eps','-deps')
To save the current image to a eps Documents orprint('graph1.png','-dpng')
Save the current image as a png file .
3、 ... and 、 Advanced drawing
- Octave Its drawing function is not only to draw a simple curve in a two-dimensional rectangular coordinate system . By calling GNUPLOT, It can draw Bar chart ,3D The surface of the , Contour map and polar coordinate map, etc
3.1 Draw a subgraph
To create multiple pictures in the same window, go through
subplot
Command implementation . This command can separate the window into a series of sub windowsIts basic grammar is
subplot(row,columns,select)
- among subplot Parameter specifies the sequence of the current drawing in the sub window
- The sequence number of the sub window is from top to bottom , The order increases from left to right
3.2 3D drawing
Octave It also provides a variety of 3D Data visualization tool , The simplest 3D The drawing implementation is
plot3
commandThis command accepts input by x,y and z Axis of the data , Draw points or lines
stay 3D In drawing xlabel and ylabel Use as usual , And new zlabel The order can be z Axis naming . And drawing 3D Picture time , The option of style of table line can also be used .
Adjust the perspective :
If you want to change 3D Perspective of drawing results , The simplest way is to use the mouse ( Click and drag with the mouse to rotate the image , See table for specific operation options ).
and Octave Of view Commands can quantitatively determine a specific perspective ( Use help view Get more Help information )
Mouse action operation LMB+ motion +LMB + motion Shift perspective Transfer axis Shortcut key operation → Right hand ↑ Topspin ← left-handed ↓ Downward rotation
3.3 3D drawing —— Draw surfaces
Define a binary function f(x, y), The surface of this function can be represented by a series of Octave Draw . First , To initialize a grid point , use meshgird Command implementation :
Such as drawing :f(x, y) = (x− 3)^2 − (y− 2)^2
Four 、 Other commands
4.1 Load and save data
When you quit Octave When , You will lose the variables you created . If you need to quit halfway through your work Octave, So you can To save the data of the current session and reload it later
save anyname
Store the variables in the entire workspace in the current directory with a name of anyname.mat In the file of ( Be careful .mat yes octave Automatically added ). So you can quit Octave, Then restart Octave Program , By inputload anyname
The previously saved namespace will be reloaded , And start working again from where you left off
Octave You can also import data from files
You can load or save specific changes The amount . The format is :
save filename var1 var2 ...
- for example , If you want to store deg Variable , Then you can enter
save degconv deg
This will deg Variables are stored in a file named degconv.mat In the file of , You can reload variables with the following commandload degconv
- for example , If you want to store deg Variable , Then you can enter
4.2 Repeat the previous order
- Octave It will record the commands you have entered in a session , And you can pass In the direction key ↓ and ↑ To view the previously entered command ( The most recent commands are at the top ). If you want to repeat one of these commands , You just need to use the arrow keys to find the command and press enter .
- Once an executed command is recalled , You can edit it before executing it again . You can use it. ← and → Move the cursor Go in parallel Line corresponding Edit operation
4.4 Get help
If you don't know someone Octave Command function or you need to find a specific function , have access to help Order to see help
help commandname
If you don't know the specific function name , From the command line Input
help -i
,Octave A list of topics for their help will be givenDetails about a help topic can be obtained by moving the cursor to the topic and entering . You can go through Up,Next, Previous Wait for shortcut keys to browse the corresponding topic . Input ‘q’ To exit the help system and return to octave Command line
4.5 Cancel a command
Ctrl-C
: Abort the program and return to the command prompt interface
if necessary Octave Use the tutorial detailed documentation , You can send me... Directly , Free to share
边栏推荐
- NVIDIA可编程推理加速器TensorRT学习笔记(一)——开始
- C language: leak detection and filling (II)
- [machine learning] is it necessary to balance samples in machine learning
- Pytorch (IV) -- pytorch model definition
- Http实战之缓存、重定向
- Talk about how programmers improve their writing ability
- 解决 error: Your local changes to the following files would be overwritten by merge:XXXX
- R Plot添加中文及其他字体【showtext】
- bom操作——缓动动画
- ABAP语法基础2
猜你喜欢
随机推荐
Advanced C language: data storage (floating point)
ABAP语法基础3
ABAP语法基础2
Kuzaobao: summary of Web3 encryption industry news on July 21
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
mysql
Do you have to follow flush privileges after MySQL grant?
Notes and Thoughts on the red dust of the sky (I) all results are conditional possibilities
dom——事件代理
51单片机外设篇:按键
cookie和seesion的区别和联系
The difference and connection between cookies and seesion
c语言进阶:字符串函数及模拟实现
Cache and redirection in HTTP practice
Android interview question: tell me the difference between pendingintent and intent
【Flutter组件】Expanded详解
5.zabbix创建自定义key
实习打怪之路:ES6中的Sym详解
美团二面: Redis 5 种基础数据结构?
Go语言 Go命令行工具