CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
DOCUMENTE SIMILARE |
|||||
|
|||||
MathCad Programming (Works with Mathcad Professional Only)
Programs make it easy to do tasks that may be impossible or very inconvenient to do in any other way. This is because a program has many attributes associated with programming languages including conditional branching, looping constructs, local scoping of variables, error handling, and the ability to call itself recursively.
What is a program
What is a program?
A program is simply an expression made up of more than one statement (available in Mathad Professional only). If you click on the examples below, you'll see how they look when written as a program rather than as a single expression.
|
Despite the underlying equivalence between programs and simple expressions, programs offer two distinct advantages:
When you use control structures like loops and conditional branches, a program can become far more flexible than a simple expression could ever be.
A program made of several simple steps is often much easier to create than an equivalent, but far more complicated expression draped with parentheses.
Defining a program (Mathcad Professional only)
To following steps illustrate how to define a the program
|
Type the left side of a function definition followed by the assignment operator ':'.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
|
Click the 'Add Line' button or press ]. This creates a vertical bar
|
with a placeholder for an additional program statement (a program can have any number of statements).
|
Click on the top placeholder, type 'z' and click on the local assignment button.
(Be aware that the definition of z is local to the program. It is undefined outside the program and has no effect anywhere but inside the program. You cannot use the Mathcad's usual assignment operator ':=', inside a program. You must use the local assignment operator represented by '<-'.)
Complete the local assignment by typing 'x/w' in the placeholder to the arrow's right.
The last placeholder should always contain the value returned by the program. Type 'log(z)' into this placeholder.
You can now use this function just as you would any other function or evaluate it symbolically.
Conditional statements (Mathcad Professional only)
|
Use a conditional statement whenever you want a program statement to execute only upon the occurrence of some condition as in the following program
To insert a conditional statement:
Click on the placeholder into which you want to place the conditional statement.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
Click the 'If' button or press Shift+]. Do not just type the word 'if'.
In the right placeholder, type a boolean expression.
Click the 'Add Line' button to insert placeholders for additional statements if necessary.
Click in the remaining placeholder and click the 'otherwise' button. Do not just type the word 'otherwise'.
In the remaining placeholder type the value you want the program to return if the condition is false.
Note:
If you use more than one 'if' statement before an 'otherwise' statement, the 'otherwise' statement is executed only when all the conditions are false.
Program loops
A loop is a program statement that causes one or more statements (the body of the loop) to execute repeatedly until a particular condition occurs. There are two kinds of loops:
'For' loops are useful when you know exactly how many times the body of the loop should execute.
'While' loops are useful when you want to stop execution upon the occurrence of a condition but you don't know exactly when that condition will occur.
When using loops, you may need to break out of them or control particular iterations.
'FOR' LOOPS
Use a 'for' loop when you know exactly how many times you want the body of the loop to execute.
|
To insert a 'for' loop
Click in the placeholder into which you want to place the 'for' loop.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
Click on the 'for' button or press Ctrl+'. Do not just type the word 'for'.
In the placeholder to the left of the 'e', enter the iteration variable.
In the placeholder to the right of the 'e', enter the range of values to be taken by the iteration variable (although you will most often use a range variable here, you can also use a vector, a list of scalars, and vectors separated by commas).
Click the 'Add Line' button to insert placeholders for additional statements if necessary. If you want the body of the loop to execute until the occurrence of a condition but you don't know exactly how many times this will take, use a 'while' loop instead.
'WHILE' LOOPS
Use a 'while' loop whenever you want a set of statements to keep executing until a condition is met. Make sure you have a statement somewhere that makes the condition false. Otherwise the loop will execute indefinitely and you will need to stop it by pressing Esc.
|
To insert a 'while' loop:
Click in the placeholder into which you want to place the 'while' loop.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
Click on the 'while' button or press Ctrl+]. Do not just type the word 'while'.
In the placeholder to the right of the 'while', type a boolean expression. Click the 'Add Line' button on the Programming toolbar to insert placeholders for additional statements if necessary.
In the placeholder below the 'while', enter the statement you want to execute repeatedly. Use the 'Add Line' button to insert placeholders for additional statements if necessary.
'While' loops are useful when you want to stop execution upon occurrence of a condition and you don't know exactly when that condition will occur. If you know exactly how many iterations you want, use a 'for' loop instead.
'BREAK' STATEMENT
Use a 'break' statement in a loop whenever you want to halt execution in a loop.
|
To insert a 'break' statement:
Click in the placeholder in which you want to put the 'break' statement.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
Click on the 'break' button or press Ctrl+{. Do not just type the word 'break'.
When Mathcad encounters a 'break' statement in the body of a "for" or "while" loop:
The loop ceases execution and return the most recent value computed.
The program execution then continues with the next line of the program after the loop.
Controlling iterations in a loop
Controlling iterations in a loop
Program loops are designed to continue until a certain condition is met or after a certain number of iterations. However, you may want to halt a loop during a certain iteration and continue with the next iteration. To do so, use a "continue" statement.
To insert a 'continue' statement:
|
Click on a placeholder into which you want to place the 'continue' statement.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
Click on the 'continue' button or press Ctrl+[. Do not just type the word 'continue'.
When the program encounters a "continue," it halts the iteration, goes to the nearest outer loop, and continues with the next iteration.
Returning a value from a program
By default, a program returns whatever is on the last line. However, you can return a value located elsewhere in the program by using a "return" statement.
|
To insert a 'return' statement:
Click on the placeholder into which you want to place the 'return' statement.
Click on the Math Toolbar to open the Programming Toolbar containing the Programming Operator.
Click on the 'return' button or press Ctrl+|. Do not just type the word 'return'.
In the placeholder to the right of the 'return', type whatever you would like to return.
'Return' statements are useful when you want to return a value from a particular loop.
Trapping errors
Trapping errors in a program
|
To return an alternative value when an error is encountered in an expression, use the "on error" programming operator:
Click on the placeholder into which you want to place the 'on error' statement.
Click on the Math toolbar to open the Programming toolbar containing the programming operators.
Click on the 'on error' button or press Ctrl+'. Do not just type the words 'on error'.
In the placeholder to the right of the 'on error', type whatever you would like to return, assuming it can be evaluated successfully.
In the placeholder to the left of the 'on error', type whatever you would like to return if the default return expression cannot be evaluated. Use the 'Add Line' button to insert placeholders for additional statements if necessary.
The right-hand expression is evaluated and returned if no errors occur. If an error occurs, the left-hand argument is returned.
|
Recursion
Recursion is a powerful programming language that involves defining a variable in terms of itself as shown in this example:
Recursive function definitions should always have at least two parts:
An initial condition to prevent the recursion from going forever, and
A definition of the function in terms of a previous value of the function.
The idea is similar to that underlying mathematical induction: if you can get f(n+1) from f(n), and you know f(0), then you know all there is to know about f. Keep in mind however, that although recursive function definitions, despite their elegance and conciseness, are not always the most computationally efficient definitions. You may find that an equivalent definition using one o f the iterative loops will evaluate more quickly.
Evaluating a program symbolically
Once you've defined a program that you want to evaluate symbolically:
(1) Type the name of the function or variable name which is defined in terms of a program.
(2) Press Ctrl +Period to insert the right arrow representing the symbolic equal sign.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 1299
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved