Solving MatLab "for" Loops With Vectors

Click For Summary

Discussion Overview

The discussion centers around solving a homework problem in MatLab involving the evaluation of a quadratic function using both "for" loops and vectorized operations. Participants explore the implementation details, syntax issues, and plotting results for the function y(x) = x^2 - 3x + 2 over a specified range.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial attempt at using a "for" loop to compute values of y and plot them, but encounters issues with the resulting graph.
  • Another participant points out that the variable x is treated as a scalar within the loop, which affects the plotting of the results.
  • A suggestion is made to plot using the range directly instead of the scalar value of x, which may resolve the plotting issue.
  • It is noted that the use of ".^" is unnecessary in the context of the "for" loop since x is scalar.
  • A later reply provides a revised code snippet that aims to improve efficiency and clarity by using indexing with a loop to compute y values.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to the use of scalars versus vectors in MatLab, but there are varying opinions on the best approach to implement the solution. The discussion remains unresolved regarding the most efficient coding practices.

Contextual Notes

Some limitations include potential misunderstandings about how variables are treated in loops versus vectorized operations, as well as the implications of using different plotting commands. The discussion does not resolve these nuances.

Who May Find This Useful

This discussion may be useful for students learning MatLab, particularly those working on homework involving loops and vector operations, as well as those interested in plotting functions in a programming context.

sirclash
Messages
7
Reaction score
0
MatLab "for" loops

Homework Statement


Write an m-file to evaluate y(x)= x^2 - 3x +2 for all values of x between -1 and 3, in steps of .1 . Do this twice, once with a for loop and once with vectors. Plot the resulting functions.


Homework Equations





The Attempt at a Solution


count=0;
for x= -1:.1:3
count = count+1;
y(count)= x.^2 -3*x +2;

end
plot(x,y)

z=-1:.1:3;
u= z.^2 -3*z + 2;
plot(z,u)

My vector is correct, however i have no clue why my "for" loop won't show the correct graph.
 
Physics news on Phys.org


the problem is that x is a scalar, so you're plotting (3, {y_1,y_2,...y_n}) which makes no sense.

Instead, try this: plot(-1:.1:3,y)

also, inside your for loop, you don't need ".^". You can just use "^" since x is scalar there.
 


well, when you use the "for" part and say for x = -1:.1:3 . MATLAB only uses this for the loop. it does not make a vector out of it. well, i think it just keeps changing the value of x so that the value of x is the value for your last loop
try putting x = -1:.1:3 after your loop and keeping the plot(x,y)
 
Last edited:


sirclash, here check this code. It can clean up your code and make it a lot more efficient

Code:
%plot x, y
x= -1:.1:3;

for i=1:length(x)
y(i)= x(i)^2 -3*x(i) +2;
end

plot(x,y)

%plot z, u
z=-1:.1:3;
u= z.^2 -3*z + 2; 
plot(z,u)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K