octave基本指令5

时间:2023-12-06 16:41:08

octave基本指令5

控制语句

for
>> v=zreos(5,1)
v =
0
0
0
0
0
>> for i=1:10,
> v(i) = 2^i;
> end;
>> v
v=
2
4
8
16
32 >> indices = 1:5;
>> for i = indices,
> disp(i);
> end;
1
2
3
4
5
while
>> i = 1
>> while i<=5,
> v(i) = 100;
> i = i + 1;
> end;
>> v
v =
100
100
100
100
100 >> i = 1;
>> while true,
> v(i) = 999;
> i = i + 1;
> if i == 6,
> break;
> end;
> end;
>> v
v =
999
999
999
999
999
if
>> v(1)
ans = 999
>> v(1) = 2;
>> if v(1) == 1;
> disp('The value is one');
> elseif v(1) = 2,
> disp('The value is two');
> else
> disp('The value is not one or two');
> end;
the value is two

定义函数

在 Octave 环境下定义的函数 你需要创建一个文件 用你的函数名来命名 然后以 .m 的后缀结尾

  • squareThisNumber.m

    function y = squareThisNumber(x)

    y = x^2;

  • 如果键入SquareThisNumber(5) 返回值是25

  • 使用 addpath 命令添加路径 添加路径

    addpath('C:\Users\ang\desktop')

  • 返回多个函数值

    function [y1, y2] = squareAndCubeThisNumber(x)

    y1 = x^2;

    y2 = x^3;