MATLAB help

  • MATLAB
  • Thread starter tironel
  • Start date
  • #1
11
0

Main Question or Discussion Point

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
 

Answers and Replies

  • #2
33,511
5,194
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
11
0
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
33,511
5,194
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
11
0
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
33,511
5,194
Here's a while loop.

Code:
x = -5.0
while x <= 5.0
  % do something
  x = x + 0.1
end
 
  • #7
11
0
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
11
0
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
33,511
5,194
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.
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 [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:
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
11
0
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 Threads on MATLAB help

  • Last Post
Replies
7
Views
3K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
6
Views
7K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
3
Views
5K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
4K
Top