Solving MatLab "for" Loops With Vectors

In summary, the conversation discussed writing an m-file to evaluate a function, y(x) = x^2 -3x +2, for all values of x between -1 and 3 in steps of 0.1, using both a for loop and vectors. The correct code was provided, which involved creating a vector for x and using a for loop to calculate the values of y for each x value. The conversation also addressed a potential issue with plotting the results using the for loop and suggested a more efficient code to achieve the desired result.
  • #1
sirclash
7
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
  • #2


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.
 
  • #3


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:
  • #4


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)
 
  • #5
Is there something wrong with my code or is there a better way to solve this problem?
Your code for the "for" loop looks correct, so there may be an issue with how you are plotting the results. Make sure you are plotting the correct variables (x and y) and that you are using the "hold on" command to plot both the "for" loop and vector results on the same graph. Also, double check that you are using the correct syntax for the plot command. A possible alternative solution could be to use the "linspace" function to create a vector of evenly spaced values between -1 and 3, and then use element-wise operations to evaluate the function for each value in the vector. This may be a more efficient way to solve the problem.
 

1. How do I create a "for" loop in MatLab using vectors?

To create a "for" loop using vectors in MatLab, you can use the "for" keyword followed by a variable name, an equal sign, and the keyword "1:length(vectorname)". This will iterate through the vector and perform the desired operation for each element.

2. How can I access elements in a vector within a "for" loop?

To access elements in a vector within a "for" loop, you can use the variable name followed by the index of the element in square brackets. For example, if your vector is named "myVector" and you want to access the third element, you would use "myVector(3)".

3. Can I use "for" loops with multiple vectors in MatLab?

Yes, you can use "for" loops with multiple vectors in MatLab. You can use the "for" loop to iterate through one vector while accessing elements from another vector within the loop.

4. How do I update values in a vector within a "for" loop?

To update values in a vector within a "for" loop, you can use the variable name followed by the index of the element in square brackets, and then use an equal sign to assign a new value to that element. For example, if you want to update the third element in a vector named "myVector", you would use "myVector(3) = newValue".

5. Can I use "for" loops to perform calculations on vectors in MatLab?

Yes, you can use "for" loops to perform calculations on vectors in MatLab. Within the loop, you can use the variable name to access and manipulate elements in the vector, and then use the updated values to perform calculations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
943
  • Engineering and Comp Sci Homework Help
Replies
1
Views
890
  • Engineering and Comp Sci Homework Help
Replies
1
Views
960
  • Engineering and Comp Sci Homework Help
Replies
3
Views
815
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
864
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top