Solving for y with a While Loop

  • Context: MATLAB 
  • Thread starter Thread starter tironel
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a while loop in MATLAB to solve for the variable y using the equation y = mx + b, where m and b are constants. Participants are exploring the correct syntax and structure for using anonymous functions and loops in MATLAB, as well as addressing issues related to plotting results.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in using a while loop to define y based on given parameters for x, m, and b.
  • Another participant suggests that the while loop syntax is incorrect and proposes using a for loop instead for evaluating y over the range of x.
  • A participant clarifies that m and b are constants while x is a variable, and emphasizes the need for a loop to compute y for each x value.
  • There is a suggestion to declare an anonymous function for y and to ensure that m, b, and x are defined before using them in the function.
  • One participant shares their code but notes discrepancies between the results of their while loop and for loop implementations, seeking advice on corrections.
  • Another participant provides a code snippet for a while loop but does not specify what to do within the loop, indicating a need for further clarification.
  • Issues with indexing in a for loop are raised, with suggestions for proper syntax and structure to avoid errors.
  • A participant expresses gratitude for assistance received, indicating that they have completed their assignment but still have questions about plotting the results correctly.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to implement the while loop, with some advocating for its use while others suggest that a for loop may be more appropriate. There is also uncertainty regarding the correct syntax for indexing and function definitions.

Contextual Notes

Participants mention specific requirements from their assignment, including the need to use both while and for loops, and the conditions under which y values should be constrained. There are unresolved issues regarding the correct implementation of these loops and the expected output.

Who May Find This Useful

This discussion may be useful for students learning MATLAB, particularly those working on assignments involving loops and function definitions in programming contexts.

tironel
Messages
10
Reaction score
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
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.
 
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
 
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.
 
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.
 
Here's a while loop.

Code:
x = -5.0
while x <= 5.0
  % do something
  x = x + 0.1
end
 
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')
 
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
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
3
Views
2K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
0
Views
2K