Problem with conditional statement in Mathematica

In summary, a conditional statement in Mathematica is a programming construct that uses the keyword "If" to specify actions based on certain conditions. The basic syntax for writing conditional statements is If[condition, action1, action2], where the condition is an expression that evaluates to True or False. Multiple conditions can be included using "And" or "Or" operators, and the "Else" keyword can be used for situations where none of the conditions are met. Variables can also be used in conditional statements, but it is important to ensure they have been defined and assigned a value, or to use the "Evaluate" function to force evaluation.
  • #1
projektMayhem
4
1
I am trying to iterate a solution to a non-linear differential equation - but I am unable to evaluate a statement within a Do loop

Code:

Do[q = Cos[Evaluate[x'/.up]]; If[ q <= 1, Print[q], Print, Print[i^2]], {i, 0.1, 1, .1}]

x' is a solution to NDSolve

The output is i^2, which is absurd as q is necessarily <= 1.

In other words, it's not properly evaluating the conditional statement. I am lost as what to do with this, any help is greatly appreciated.

edit: I'm using Mathematica 5.0
 
Physics news on Phys.org
  • #2
Nevermind, problem resolved.
 

FAQ: Problem with conditional statement in Mathematica

What is a conditional statement in Mathematica?

A conditional statement in Mathematica is a programming construct that allows you to specify actions to be taken based on certain conditions being met. It is commonly used in decision-making processes and is denoted by the keyword "If".

What is the syntax for writing conditional statements in Mathematica?

The basic syntax for writing conditional statements in Mathematica is:
If[condition, action1, action2]
The "condition" is an expression that evaluates to either True or False, and the "action1" and "action2" are the actions to be taken if the condition is met or not met, respectively.

Can I have multiple conditions in a single conditional statement?

Yes, you can have multiple conditions in a single conditional statement by using the "And" or "Or" operators. For example:
If[condition1 && condition2, action1, action2]
If both condition1 and condition2 evaluate to True, then action1 will be taken. Otherwise, action2 will be taken.

How can I handle situations where none of the conditions in my conditional statement are met?

You can use the "Else" keyword to specify an action to be taken if none of the conditions in your conditional statement are met. For example:
If[condition1, action1, If[condition2, action2, Else, action3]]
If neither condition1 nor condition2 are met, then action3 will be taken.

Can I use variables in my conditional statement?

Yes, you can use variables in your conditional statement. However, it is important to make sure that the variables have been defined and assigned a value, otherwise the condition may not evaluate properly. You can also use the "Evaluate" function to force the evaluation of a variable in a conditional statement.

Similar threads

Replies
1
Views
692
Replies
2
Views
1K
Replies
6
Views
3K
Replies
5
Views
2K
Replies
4
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Back
Top