"Do" loop with "If" conditional

In summary, the conversation discusses using a Do loop to calculate the value of a function for specific regions of x, y, z. The speaker suggests using Table instead of Do and using an If statement to add a constraint on the function. They also mention using Export to save the output in a file. They offer a suggestion for creating the table and mention the possibility of automating the constraint.
  • #1
Monaliza Smile
6
0
Hi,

I have a function defined by:

F[x_,y_,z_]:=3 x+y-2Y[z]

where
Y[z_]:=3/z

I'd like to run Do loop to calculate the value of this function for specific regions of x,y,z.

I can use:

Do[F[x,y,z],{x,1,2,1},{y,-2,-1,1},{z,3,5,1}],

But I have two questions here:

First: How to save the output in .dat file ? I 'd like to have something like:

F ##~~~~## x ##~~~~## y ##~~~~## z

Second: How Do loop can contain a conditional If, to put constrain on Y[z] -> 0 <Y[z] < 1, before evaluating F.
 
Last edited:
Physics news on Phys.org
  • #2
You will be better off to use Table here rather than Do.

Just put the If inside the definition of Y[z_]
 
  • #3
With the export-to-file in mind I'd suggest creating the table as follows

Code:
data = Table[ If[ Y[z]>0 && Y[z]<1, {F[x,y,z], x, y, z}, 0 ], {x,1,2,1}, {y,-2,-1,1}, {z,3,5,1}] // DeleteCases[0];

Next you can use Export[ file, data, format].

Remark;
1. I'm not sure what would happen if you leave the else-case empty in the If[] statement. Perhaps it doesn't add an element or maybe a zero.
I'm lazy so I went for a way that certainly works.
2. For ranges like ##\{x,1,2,1\}## you don't have to add the step size if it is one.

Question;
Is the function ##Y[z_]## always the same? Then you can simply solve the constraint and avoid it.
Otherwise you could try to automate it by solving the inequalities and using the answer in the range.
 

1. What is a "Do" loop?

A "Do" loop is a programming structure that allows a set of instructions to be repeated until a certain condition is met. It is also known as a "do-while" loop because it continues to execute as long as the specified condition is true.

2. How is a "Do" loop different from other types of loops?

A "Do" loop is unique because it executes the code block at least once before checking the condition. This is in contrast to a "while" loop, which checks the condition first before executing the code block.

3. What is the purpose of using an "If" conditional within a "Do" loop?

The "If" conditional allows for more control over the code within the "Do" loop. It allows the loop to continue or terminate based on a specific condition, giving the programmer more flexibility and control over the loop's execution.

4. Can an "If" conditional be used with other types of loops?

Yes, an "If" conditional can be used with other types of loops, such as "for" and "while" loops. It is a common practice to use conditionals within loops to control the flow of the program.

5. What happens if the condition in the "If" statement is not met?

If the condition in the "If" statement is not met, the loop will either terminate or continue depending on the code block within the loop. It is important to ensure that the condition is well-defined to avoid unintended results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
150
  • Calculus and Beyond Homework Help
Replies
1
Views
468
  • Precalculus Mathematics Homework Help
Replies
4
Views
528
  • Programming and Computer Science
Replies
3
Views
369
  • Topology and Analysis
Replies
1
Views
785
Replies
1
Views
800
Back
Top