Posted By:
31 Mar, 2013 7:06 am PDT

Use a for loop to evaluate the function using Matlab

I have a function y that I want to calculate using matlab
y = e^(x+1) if x<-1
y = 2+cos(x) if -1<=x<5
y = 10(x-5) +1 if x>=5

I want to plot the value of y as a function of x

Attachment:None


% y = e^(x+1) if x<-1
% y = 2+cos(x) if -1<=x<5
% y = 10(x-5) +1 if x>=5


x = -10:.01:10;
for i = 1:length(x)
    if(x(i)<-1)
        y(i) = exp(x(i)+1);
    elseif(x(i)<5)
        y(i) = 2+cos(x(i));
    else
        y(i) = 10*(x(i)-5)+1;
    end
end

plot(x,y)
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud