Skip to main content
1-Visitor
March 19, 2013
Solved

if statements during iterations

  • March 19, 2013
  • 8 replies
  • 7825 views

Hi everyone,

I am programming a Newmark's Method that involves 3 masses. During the process I need to fill a matrix that represents how the stiffness vary along time, and well at that point I have a problem. I don't really know how Mathcad solves de flow of the iteration, I mean, I don't know if I can ask it to do an operation with a previously calculated value, but I need to do it.

Clipboard01.jpg

The error is "this value must be a scalar" all the variables involved in that "if" are scalars so I don't know why it's not working.

I really appreciate your help. Thanks in advance.

PS: Excuse me for my poor english, I'm Spanish.

Best answer by Werner_E

Similar answer as in the previous thread:

Lets begin with the error message:

You can use previously calculated values in a program, but ...You compare a difference of x with y.2c. x is a vector whose elements are three element vectors, so x[i-x[i-1 is a vector while y.2c is a scalar. Thats throwing the error. Whats the purpose of that comparison? Would you like the absolute value of the difference of those vectors to be smaller than the y values?

The break statement can never trigger as you are asking if a three element vector (x[i) is equal to a scalar (R). Again it does no harm but could be deleted. Don't know what the purpose of that break should have been, but you have be aware that it will only break out of the j-loop.

The statements to the left of your four if statements should be instructions to be executed if the condition to the right of the if is true. In your program these are boolean expressions themselves. Think they should be local assignments.

I think that there is an error in condition of the four if-statements. The first compares the difference of x[i and x[i+1 while the next three would compare the diff between x[i and x[j.

Strongly think that this should be the same for all four. If you use j you run into troubles as j is runníng from 1 to 3 and at the beginning 2 and three are invalid indices for x. If i+1 is meant, then the j-loop would be useless and should be omitted. The same calculations are done three times which doesn't make sense. Does no harm but slows down without benefit

See attached. This time I haven't deleted the j-loop and the break statement.

BTW, if I am right concerning the 4 if statements you could delete the four of them and replace them with a single line (see below).

newmark7.png

Edited: While the one-liner above is correct its not so easy to read and comprehend. Have it replaced by two if/otherwise statements.

8 replies

1-Visitor
March 19, 2013

Your statements like "(K2=K21+K22)" and so on should use the assignment operator <- (left pointing arrow thing). This is how values are given to variables in program statements/loops.

They should look like "(K2<-K21+K22)" and so on.

Also note that x[0 is a 3x1 matrix going into the loop, which is causing your "scalar" error message. I think you want x[0 to be a single value. The "trace()" function will show this.

1-Visitor
March 19, 2013

Maikel Nite wrote:

PS: Excuse me for my poor english, I'm Spanish.

I can only wish that my Spanish was as good as your English.

Werner_E
Werner_E25-Diamond IAnswer
25-Diamond I
March 19, 2013

Similar answer as in the previous thread:

Lets begin with the error message:

You can use previously calculated values in a program, but ...You compare a difference of x with y.2c. x is a vector whose elements are three element vectors, so x[i-x[i-1 is a vector while y.2c is a scalar. Thats throwing the error. Whats the purpose of that comparison? Would you like the absolute value of the difference of those vectors to be smaller than the y values?

The break statement can never trigger as you are asking if a three element vector (x[i) is equal to a scalar (R). Again it does no harm but could be deleted. Don't know what the purpose of that break should have been, but you have be aware that it will only break out of the j-loop.

The statements to the left of your four if statements should be instructions to be executed if the condition to the right of the if is true. In your program these are boolean expressions themselves. Think they should be local assignments.

I think that there is an error in condition of the four if-statements. The first compares the difference of x[i and x[i+1 while the next three would compare the diff between x[i and x[j.

Strongly think that this should be the same for all four. If you use j you run into troubles as j is runníng from 1 to 3 and at the beginning 2 and three are invalid indices for x. If i+1 is meant, then the j-loop would be useless and should be omitted. The same calculations are done three times which doesn't make sense. Does no harm but slows down without benefit

See attached. This time I haven't deleted the j-loop and the break statement.

BTW, if I am right concerning the 4 if statements you could delete the four of them and replace them with a single line (see below).

newmark7.png

Edited: While the one-liner above is correct its not so easy to read and comprehend. Have it replaced by two if/otherwise statements.

1-Visitor
March 20, 2013

Really really useful these answers.

In particular I'm really amazed with your answer Werner.

I'm checking the programme, and now I have more clear a couple of things, I've understood why it wasn't working the xi-xi+1 operation. It's because I really wanted to do the operation that its showed in the picture that I've attached, I wanted to pick up the second component of the x vector and substract it from the first one in each iteration and compare the result with the y2c. To do so I've tried to traspose the vector and do the diference.

While it is outside the program it works as I want but when it is inside, Mathcad says that the value must be a scalar. Any clues?

Clipboard03.jpg

PS. I know that I've said before, but I really appreciate your help

1-Visitor
March 20, 2013

Attaching your worksheet will help people see what you are doing.

I strongly suggest you do this.