怎么用matlab画出下面分段函数的图像?

2024-12-03 21:36:15
推荐回答(4个)
回答(1):

%% forfavonian9


n = -10:1:10;

x = zeros(size(n));

for i = 1:21;

    if n(i) >= -4 && n(i) <= -1

        x(i) = 2 * n(i) + 10;

    elseif n(i) >= 0 && n(i) <= 4

    x(i) = 6;

    else

        x(i) = 0;

    end

end

plot(n,x,'.-')


A = [1 2 3;4 5 6;7 8 9; 10 11 12];

[v,i] = max(A,[],1);


回答(2):

myfun = @(x) (2*x+10).*(x>=-4 & x<=-1)+6.*(x>=0 & x<=4);


n=-10:10;

y=myfun(n);

plot(n,y,'r-o')


回答(3):

n=-10:1:10;

for k=1:length(n)

    if n(k)>=-4&&n(k)<=-1

        x(k)=2*n(k)+10;

    else if n(k)>=0&&n(k)<=4

            x(k)=6;

        else

            x(k)=0;

        end

    end

end

plot(n,x,'b:*')

回答(4):

myfun = @(x) (2*x+10).*(x>=-4 & x<=-1)+6.*(x>=0 & x<=4);
fplot(myfun)