"Do" loop with "If" conditional

Click For Summary
SUMMARY

The discussion focuses on implementing a "Do" loop with an "If" conditional in Mathematica to evaluate a function defined as F[x_,y_,z_]:=3 x+y-2Y[z], where Y[z_]:=3/z. The user seeks to save the output in a .dat file and apply constraints on Y[z] to ensure 0 < Y[z] < 1 before evaluating F. The recommended approach is to utilize the Table function with an If statement to filter results, followed by exporting the data using Export[file, data, format].

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with the Table function in Mathematica
  • Knowledge of conditional statements in programming
  • Experience with file I/O operations in Mathematica
NEXT STEPS
  • Learn how to use the Export function in Mathematica for different file formats
  • Explore advanced conditional statements in Mathematica
  • Investigate the use of the DeleteCases function for data manipulation
  • Study the implications of defining functions with constraints in Mathematica
USEFUL FOR

Mathematica users, data analysts, and programmers looking to perform conditional evaluations and export results in computational tasks.

Monaliza Smile
Messages
5
Reaction score
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
You will be better off to use Table here rather than Do.

Just put the If inside the definition of Y[z_]
 
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.
 

Similar threads

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