Matlab程序设计

Matlab软件程序编写何C语言差不多,一般包含变量输入,数据处理何结果输出三大模块

  数据处理模块分为

    顺序,选择,循环三个结构

1,Matlab软件程序的输入,输出方式

  (1)输入方式

    直接输入

    x=1

    A=[1 2;3 4]

    s='haha'    %赋值字符串

    

    input('')    %里面必须有‘’

    x=input('please')  %输入的是数据

    x=input('please','s')    %'s'表示输入的是字符串

  (2)输出

    fprintf  格式控制输出

x=pi;y=sqrt(2);
fprintf('x=%.6f,y=%.0f',x,y)

      

    其中'\n'是换行

2,Matlab中循环结构与应用

  for-end格式 

    for n=n1:(step):n2

      commands-1

    end    %step默认为1

s=0;        %s必须要初始化
for n=1:1:100
    s=s+n;
end
fprintf('%d\n',s)     %不像python,必须需要格式

 

 

  format rat 分数形式

format rat
x=0.1

 

  while-end格式

  while(condition)

    commands;

  end

s=0;n=1;
while(s<1000)
    s=s+sqrt(n);
    n=n+1;
end
fprintf('%d\n',n)

 

 3,Matlab条件判断语句

  先附上一个表

  

 

  (1)

    if <条件表达式>

      语体1

    end

  (2) if <条件表达式>

      语体1

    else 

      语体2

    end

x=input('');
if x>1
    s=1
else 
    s=2
end

 

 

  (3)if <条件表达式1>

     语句体1

   elseif <条件表达式2>

     语句体2

   else

     语句体3

   end

x=input('请输入=');
if x==1
    s=1;
elseif x==2
    s=2;
else
    s=3;
end
fprintf('%d\n',s);

 

 4,Matlab函数调用

  

function fn=Fibonacci(n)
if n==1
    fn=1;
elseif n==2
    fn=[1,1];
else
    a=Fibonacci(n-1);
    fn=[a,a(end-1),a(end)];
end

 

 

5,流程控制语句

  break终止,for-end和while-end循环

  return是结束程序

  pause是展厅,pause(n)n是暂停的时间秒数

posted @ 2020-10-30 15:50  Kaiser-  阅读(600)  评论(0编辑  收藏  举报