- #1

- 11

- 0

clc,clear,

[email protected](x,m,b)m.*x+b

while m=2.5

b=7.5

x=-.5:.1:5

end

y

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- MATLAB
- Thread starter tironel
- Start date

- #1

- 11

- 0

clc,clear,

[email protected](x,m,b)m.*x+b

while m=2.5

b=7.5

x=-.5:.1:5

end

y

- #2

Mark44

Mentor

- 34,902

- 6,646

Code:

`y = @(x) m*x. + b`

m and b should be defined before this line, though, and your x vector should also be defined before this, too.

I don't understand what you are trying to do with your while loop. Aside from that, you should have

Code:

`while m == 2.5`

Can you be more specific about what your code needs to do? It seems to me that what you need to do is evaluate y for each value of x in the interval. If that's it, a for loop is more appropriate.

- #3

- 11

- 0

- #4

Mark44

Mentor

- 34,902

- 6,646

Does your problem statement say that you have to use a while loop? You can do this with a while loop, but it would be simpler with a for loop.

As I see it, you need to do the following:

1. Set m

2. Set b

3. Initialized the x vector.

4. Use a loop (of some kind) and your anonymous function to assign a value to the y vector for each x value.

- #5

- 11

- 0

- #6

Mark44

Mentor

- 34,902

- 6,646

Here's a while loop.

Code:

```
x = -5.0
while x <= 5.0
% do something
x = x + 0.1
end
```

- #7

- 11

- 0

%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;

[email protected](x1) m1*x+ b1;

[email protected](x2) m2*x2+ b2;

[email protected](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')

- #8

- 11

- 0

m2=5;

b2=10;

i=1;

x2=-5:.2:5;

for i<=length(x2)

y2(i)[email protected](x2(i)) m2.*x2.+b2;

if y2<-5

y2=-5

elseif y2>5

y2=5

end

end

- #9

Mark44

Mentor

- 34,902

- 6,646

When you post code, put a [ code] tag at the beginning of your code and a [ /code] tag (both without the leading space) at the end. I've done this for you. Using these tags preserves your formatting and makes the code easier to read.I was told i need to assign an index to the for loop to get my plot to come out right I have this but for some reason it does not work it has an error at i<=length x2 which i do not understand

Code:`m2=5; b2=10; i=1; x2=-5:.2:5; for i<=length(x2) y2(i)[email protected](x2(i)) m2.*x2.+b2; if y2<-5 y2=-5 elseif y2>5 y2=5 end end`

Take a look at this documentation - http://www.mathworks.com/help/techdoc/learn_matlab/f4-1931.html#brbsthm [Broken] - which discusses how to use a for loop. You could do something like this:

Code:

```
len = length(x2)
for i = 1:len
.
.
.
end
```

Also, you aren't being consistent in your use of array indexes with y2 and x2. I believe that your line where you are calling your function should look like this:

The four lines that follow it should have y2(i) instead of just y2.

Last edited by a moderator:

- #10

- 11

- 0

for a learning experience in the future

Code:

```
m=2.5;
b=7.5;
x=-5.0;
while x<=5
y=m*x+b
x=x+.1;
end
plot(x,y)
xlabel('X-values')
ylabel('Y-values')
xlim([-5 5])
ylim([-20 40])
title('Y vs. X')
grid
```

why is my graph not posting the line I have the values of x and the values of y. I searched the help files in matlab and those from the previous post you gave me and I cannot answer my own question. Thank you once again.

Share: