Solving for y with a While Loop

  • MATLAB
  • Thread starter tironel
  • Start date
  • Tags
    Loop
In summary: You are trying to use an anonymous function in a way that it is not intended to be used. Instead of trying to assign a function to y2(i), you should just assign the value of the function to y2(i). Also, your for loop should be using the index i, not the values in x2. Here is an example of how you could do this:m2 = 5;b2 = 10;i = 1;x2 = -5:.2:5;for i = 1:length(x2)
  • #1
tironel
11
0
I need help with this while loop, i have to define x = -5:.1:5; m=2.5; b=7.5. I am supposed to use y=mx+b as an anonymous function and I need to solve for y using a while loop however I am haveing some problems this is the syntax I have, any help?

clc,clear,
y=@(x,m,b)m.*x+b
while m=2.5
b=7.5
x=-.5:.1:5
end
y
 
Physics news on Phys.org
  • #2
The Matlab Tutorial (http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf) has a description of anonymous functions on page 4-24. Your 2nd line should probably be like so:
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
not the single = as you have.

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
I'm sorry x m and b are all supposed to be set as variables that is why I had all three in the function denoted as variables and I am supposed to solve the y value using a while loop given the parameters of x m and b
 
  • #4
My understanding of this problem is that m and b are parameters, not variables (their values don't change once they've been set), and x is a variable (taking on the values -5.0, -4.9, -4.8, ..., 4.9, 5.0. The underlying function is f(x) = mx + b, where again, x is the variable, and m and b are parameters whose values don't change.

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
Declare an anonymous function y=mx+b. If you do not know how to do this look up function handles in the help document in Matlab. Let m, x, & b be variables in the function.*** I think the part confusing me is declaring m,x, and b all variables.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.----That was the problem statement word for word, however the while loop is what is giving me troubles, to me the for loop makes sense however the while loop is just throwing me off.
 
  • #6
Here's a while loop.

Code:
x = -5.0
while x <= 5.0
  % do something
  x = x + 0.1
end
 
  • #7
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')
 
  • #8
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
m2=5;
b2=10;
i=1;
x2=-5:.2:5;
for i<=length(x2)
y2(i)=@(x2(i)) m2.*x2.+b2;
if y2<-5
y2=-5
elseif y2>5
y2=5
end
end
 
  • #9
tironel said:
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
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.
tironel said:
Code:
m2=5;
b2=10;
i=1;
x2=-5:.2:5;
for i<=length(x2) 
     y2(i)=@(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 - 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:
Code:
 y2(i)=@(x2(i)) m2*x2(i)+b2;

The four lines that follow it should have y2(i) instead of just y2.
 
Last edited by a moderator:
  • #10
THANK YOU for the help! I completed my assignment and you have showed me how to start thinking to solve some MATLAB problems I am sorry I was so bothersome this is my first time to use MATLAB and my instructor gave us a homework without lecturing and does not have that great of an understanding of MATLAB and is little help when we ask him questions. THANK YOU.
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.
 

Related to Solving for y with a While Loop

1. How does a while loop help with solving for y?

A while loop is a programming structure that allows a set of code to be repeated until a certain condition is met. By using a while loop, we can repeatedly solve for y until we reach the desired value.

2. What is the syntax for using a while loop to solve for y?

The basic syntax for a while loop is: while (condition) { //code to be repeated } In this case, the condition would be the desired value for y.

3. Can a while loop be used to solve for y in any equation?

Yes, a while loop can be used to solve for y in any equation as long as the equation can be expressed in a way that the condition for the loop can be satisfied.

4. How do I know when to stop the while loop when solving for y?

The while loop will continue to run until the condition is no longer satisfied. In the case of solving for y, this would mean that the value of y has been found and the loop can be stopped.

5. Are there any limitations to using a while loop for solving for y?

There are no specific limitations for using a while loop for solving for y, but it is important to ensure that the condition for the loop is properly set and the loop does not run indefinitely.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
7
Views
754
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
332
Back
Top