Matlab For Loop Problem: Solve y=x^3+1

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

Discussion Overview

The discussion revolves around solving the equation y=x^3+1 using a for loop in MATLAB. Participants explore the implementation of for loops, the use of the dot operator, and the requirements of an assignment that specifies a uniform distribution of points over a defined range.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in understanding for loops in MATLAB and seeks guidance on solving the equation using this method.
  • Another participant questions the necessity of a for loop, suggesting that the cubic formula could be used instead and proposes moving the discussion to a more appropriate section.
  • A participant clarifies that the assignment requires the use of the dot operator and a for loop, specifying the range of x from 0 to 2 with 100 uniformly distributed points.
  • Another participant suggests using the linspace command for generating a uniformly spaced range of numbers and mentions the potential use of machine epsilon for checking proximity to zeroes of the function.
  • There is a correction regarding the use of the power operator, indicating that "i^3" is incorrect for vector operations and should be "i.^3".
  • Concerns are raised about using a non-integer index for the output array y, emphasizing the importance of using integer indices in MATLAB.
  • A suggestion is made to prefer vectorization over loops for efficiency in MATLAB programming.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solve the problem, with differing opinions on the necessity of for loops versus vectorization and the appropriateness of the assignment requirements.

Contextual Notes

There are unresolved issues regarding the correct implementation of indexing in MATLAB, the use of the dot operator, and the overall approach to solving the equation within the constraints of the assignment.

kschul14
Messages
2
Reaction score
0
I'm have a really hard time understanding for loops in matlab. How can I solve y=x^3+1 with a for loop?
 
Physics news on Phys.org
Why would you use a for loop? Are you implementing some sort of iterative algorithm? Just use the cubic formula.
This really belongs in the programming section. I'll ask one of the mods to move it.
 
it's an assignment. we are supposed to use the dot operator which is really easy and a for loop and the range is 0<=x<=2 with 100 points distributed uniformly. I have tryed the fallowing
for i=0:01:2
y(i)=1+i^3
end
this won't work
 
To get a uniformly space range of numbers on a specified interval use the linspace command.

If you are trying to find the zero(s) of the function, what is a test you can use to check if you are close?

You may also find eps, the machine epsilon, to be of use.
 
kschul14 said:
it's an assignment. we are supposed to use the dot operator which is really easy and a for loop and the range is 0<=x<=2 with 100 points distributed uniformly. I have tryed the fallowing
for i=0:01:2
y(i)=1+i^3
end
this won't work

You're trying to raise a vector to a power; using "i^3" will attempt to multiply i*i*i, which is not what you want. The correct expression is "i.^3". You should be able to figure out what to do from there.
 
Code:
for i=0:01:2
    y(i)=1+i^3
end

You are using i as an index for y, when i is not an integer. (You can't get the 0.1th element of y, for example.)

In MATLAB, you should always prefer using vectors to loops. How can you vectorize this?
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
6
Views
4K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K