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

  • MATLAB
  • Thread starter kschul14
  • Start date
  • Tags
    Loop Matlab
In summary, the conversation discusses using for loops and the dot operator in MATLAB to solve a math problem. The individual is having trouble understanding for loops and is seeking help on how to properly implement them. They also mention using the linspace command and the machine epsilon for their assignment. The conversation suggests using vectors instead of loops in MATLAB.
  • #1
kschul14
2
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
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5
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.
 
  • #6
Code:
[COLOR="Blue"]for[/COLOR] i=0:01:2
    y(i)=1+i^3
[COLOR="Blue"]end [/COLOR]

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?
 

1. What is a for loop in Matlab?

A for loop in Matlab is a programming structure that allows you to repeatedly execute a set of code for a specified number of times. It is used to iterate over a set of values or elements and perform a certain action on each one.

2. How do I initialize a for loop in Matlab?

To initialize a for loop in Matlab, you need to specify three things: the loop control variable, the starting value of the variable, and the ending value of the variable. This can be done using the syntax for variable = start:increment:end where the loop control variable is variable, the starting value is start, the increment is increment, and the ending value is end.

3. How do I solve a problem using a for loop in Matlab?

To solve a problem using a for loop in Matlab, you first need to understand the problem and determine what action needs to be repeated. Then, you can use a for loop to iterate over a set of values or elements and perform the necessary action on each one. Make sure to initialize the loop control variable and update it within the loop to avoid an infinite loop.

4. How do I solve the given problem "y=x^3+1" using a for loop in Matlab?

To solve the given problem "y=x^3+1" using a for loop in Matlab, you can use the following code:

for x = 1:10
y = x^3 + 1;
disp(y);
end

This will iterate over the values 1 to 10 for x and calculate the corresponding value of y, which will be displayed on the screen.

5. Can I use a for loop to solve more complex problems in Matlab?

Yes, for loops can be used to solve a wide range of problems in Matlab, from simple mathematical calculations to more complex data analysis tasks. With proper understanding and implementation, for loops can be a powerful tool in solving problems efficiently and effectively in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
797
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
Back
Top