Webster586
2021-08-26 20:32
采纳率: 100%
浏览 32

matlab程序设计流程控制结构

  • 收藏

1条回答 默认 最新

  • CSDN专家-Matlab_Fans 2021-08-26 20:49
    已采纳
    s = 0;
    n = 0;
    a = input('请输入数字2 3 4 0 \n');
    while a ~= 0 
        s = s + a;
        n = n + 1;
        a = input('请输入数字2 3 4 0 \n');
    end
    x = s / n
    
    %% For 循环
    x = 0:0.05:2*pi;
    y1 = sin(x);
    y2 = 0;
    for ii = 1:30
        y2 = y2 + (-1)^(ii-1)*(x).^(2*ii-1)./factorial(2*ii-1);
    end
    figure
    plot(x,y1,x,y2,'r--')
    
    %% While 循环
    x = 0:0.05:2*pi;
    y1 = sin(x);
    y2 = 0;
    ii = 1;
    while ii <= 30
        y2 = y2 + (-1)^(ii-1)*(x).^(2*ii-1)./factorial(2*ii-1);
        ii = ii+1;
    end
    figure
    plot(x,y1,x,y2,'r--')
    
    %% 函数方法
    x = 0:0.05:2*pi;
    y1 = sin(x);
    y2 = 0;
    ii = 1;
    fn = @(ii)  (-1)^(ii-1)*(x).^(2*ii-1)./factorial(2*ii-1);
    y2 = arrayfun( fn,1:30,'UniformOutput',false )
    y2 = sum(cell2mat(y2'));
    
    figure
    plot(x,y1,x,y2,'r--')
    
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题