Below is my code but my values on my graph for y2 do not match the values I get for my for loop. Any suggestions on correcting this problem??
%Declare an anonymous function y=mx+b.Let m, x, & b be variables in the
%function. Using a while loop solve for y as x ranges from -5 to 5 in
%increments of 0.1. Let m = 2.5 & b = 7.5.Using a for loop solve for y as x
%ranges from -5 to 5 in increments of 0.2. Let m = 5 & b = 10, if y < -5
%set y = -5 or if y > 5 set y = 5.Use the vectorized operations to solve
%for y. Let m = 4 & b = 7. Allow x = -5 to 5 in increments of 0.05.Plot all
%three values of y vs. x on the same line plot.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc,clear
%% Calculation 4
m1=2.5;
b1=7.5;
x1=-5.0;
while x1<=5
y1= m1*x1+b1
x1=x1+.1;
end
%% Calculation 5
m2=5;
b2=10;
for x2=-5:.2:5;
y2= m2*x2+b2;
if y2<-5
y2=-5
elseif y2>5
y2=5
end
end
%% Calculation 6
x3=[-5:.05:5];
m3=4;
b3=7;
y3= m3.*x3+b3
%% Plot
x=-5:5;
y1=@(x1) m1*x+ b1;
y2=@(x2) m2*x2+ b2;
y3=@(x3) m3*x+ b3;
plot(x,y1(x),'k-',x,y2(x),'b.',x,y3(x),'g-.','Linewidth',2)
xlabel('X-values')
ylabel('Y-values')
xlim([-5 5])
ylim([-20 40])
title('Y vs. X')
grid
legend('y1','y2','y3','Location','NW')