cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

working with vectors in a program

ptc-5020731
1-Visitor

working with vectors in a program

Hi everyone, I'm having lots of issues with one program I'm doing, and I would like to ask if you can help me with one in particular.

I'm trying to pick up a number from a vector compare it with some value and then do an iteration, but for some reason Mathcad doesn't let me pick up the number from the vector.

I'm trying to do:

Clipboard04.jpg

but it seems that I cannot use this kind of statement in an iteration.

Thx in advance.

PS: I'm Spanish so excuse me if my english it's a little bit rough

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:ptc-5020731)

I tried to edit the foregoing post and insert a pic but got repeatedly an error "null" ??

So here it is:

26.04_3.png

Is this in the range of what you tried to do?

BTW, I think that you felt in the trap of thinking that Mathcad would take the variables which are calculated and defined in the sheet before the routine in some other parts of your routine, too (K22). K23 is OK as you never define a local variable with that name.

View solution in original post

13 REPLIES 13
Werner_E
25-Diamond I
(To:ptc-5020731)

Not sure what the problem is?

v.return is a matrix consisting of four vectors, each containing 21 3*1 vectors.

Where do you try to pic a number from (which?) vector??

BTW, this

26.04.png

doesn't seem to make any sense!?

Another thing I noticed:

26.04_2.png

As soon as K1 gets 0 the first time it will never change that value again. Is this what should happen? Or did you had in mind that the local K1 should get the value of the variable K1 outside if the program whenever the condition is fulfilled? In the latter case you would have to rename one of the K1, as Mathcad will always take the "nearest" K1 it will find - tha tis the local one if it exist (which is the case after the first assignment).

BTW, this

26.04.png

doesn't seem to make any sense!?

no you're right, but this it's only because the program isn't finished yet and I have to figure out a couple of things more.

Another thing I noticed:

26.04_2.png

As soon as K1 gets 0 the first time it will never change that value again. Is this what should happen? Or did you had in mind that the local K1 should get the value of the variable K1 outside if the program whenever the condition is fulfilled? In the latter case you would have to rename one of the K1, as Mathcad will always take the "nearest" K1 it will find - tha tis the local one if it exist (which is the case after the first assignment).

I thought that Mathcad checks each step if the condition is fullfilled and if it is it takes the first definition of K1 because the vaules of x are changing each step.

Despite all of this, the problem I was talking about was with the definition of Fi+1, I want to make a simple calculus with a scalar number, I mean obtain a diferent value of Fi+1 each step but mathcad tells me that it has to be a matrix, and I don't get it.

THX

Werner_E
25-Diamond I
(To:ptc-5020731)

Another thing I noticed:

26.04_2.png

As soon as K1 gets 0 the first time it will never change that value again. Is this what should happen? Or did you had in mind that the local K1 should get the value of the variable K1 outside if the program whenever the condition is fulfilled? In the latter case you would have to rename one of the K1, as Mathcad will always take the "nearest" K1 it will find - tha tis the local one if it exist (which is the case after the first assignment).

I thought that Mathcad checks each step if the condition is fullfilled and if it is it takes the first definition of K1 because the vaules of x are changing each step.

What do you mean by "first definition"? The one outside of the program? No, it will only take it the very first time the line K1 <--- .... is executed. From then on in the iteration mathcad will take the internal variable K1 anf if this is set to zero in one step, it will stay zero for all succeeding steps. The vector x changes in each step, but why do you think this would affect K1? K1 is not calculated in your routine. It is either set to K1 (which makes sense only the very first time) or set to zero.

Despite all of this, the problem I was talking about was with the definition of Fi+1, I want to make a simple calculus with a scalar number, I mean obtain a diferent value of Fi+1 each step but mathcad tells me that it has to be a matrix, and I don't get it.

But there is no code in your file showing this attempt!? Fi+1 is a 3x1 vector, not a scalar.

Werner Exinger escribió:

Another thing I noticed:

26.04_2.png

As soon as K1 gets 0 the first time it will never change that value again. Is this what should happen? Or did you had in mind that the local K1 should get the value of the variable K1 outside if the program whenever the condition is fulfilled? In the latter case you would have to rename one of the K1, as Mathcad will always take the "nearest" K1 it will find - tha tis the local one if it exist (which is the case after the first assignment).

I thought that Mathcad checks each step if the condition is fullfilled and if it is it takes the first definition of K1 because the vaules of x are changing each step.

What do you mean by "first definition"? The one outside of the program? No, it will only take it the very first time the line K1 <--- .... is executed. From then on in the iteration mathcad will take the internal variable K1 anf if this is set to zero in one step, it will stay zero for all succeeding steps. The vector x changes in each step, but why do you think this would affect K1? K1 is not calculated in your routine. It is either set to K1 (which makes sense only the very first time) or set to zero.

Mathcad won't compare each step if xi0-xi1 is >0?

Werner_E
25-Diamond I
(To:ptc-5020731)

nges in each step, but why do you think this would affect K1? K1 is not calculated in your routine. It is either set to K1 (which makes sense only the very first time) or set to zero.

Mathcad won't compare each step if xi0-xi1 is >0?

Ofcourse it does, but what happens if the condition is fulfilled? K1 is assigned K1!? The K1 to the left of the assignment is a local variable K1. The K1 to the right is ALSO this very local variable!! Mathcad will only grab the worksheet variable calculated before if there is no local variable with the name K1 which is the case exactly one times - the first time the assigment is done. From then on the local K1 is taken for evey K1 in the program and if this is set to 0 it will stay zero for the rest of the routine as it is never assigned a different value. K1 <-- K1 will take the local K1 (which is zero) and assign it to itself - it remains zero.

Ok, Understood.

Werner_E
25-Diamond I
(To:ptc-5020731)

I tried to edit the foregoing post and insert a pic but got repeatedly an error "null" ??

So here it is:

26.04_3.png

Is this in the range of what you tried to do?

BTW, I think that you felt in the trap of thinking that Mathcad would take the variables which are calculated and defined in the sheet before the routine in some other parts of your routine, too (K22). K23 is OK as you never define a local variable with that name.

Werner Exinger escribió:

I tried to edit the foregoing post and insert a pic but got repeatedly an error "null" ??

So here it is:

26.04_3.png

Is this in the range of what you tried to do?

BTW, I think that you felt in the trap of thinking that Mathcad would take the variables which are calculated and defined in the sheet before the routine in some other parts of your routine, too (K22). K23 is OK as you never define a local variable with that name.

Now I'm really scared, Mathcad desn't use the variables I've defined before the routine?

BTW I could solve the problem with the vector and the scalar thing with that definition.

Clipboard05.jpg

THx a lot.

And now I've to think what you said about the previously defined variables...

Werner_E
25-Diamond I
(To:ptc-5020731)

Now I'm really scared, Mathcad desn't use the variables I've defined before the routine?

Not, if a local variable with the same name exist. It will take the "nearest" variable it finds.

Thats pretty much the same behaviour as with most programming languages

While in some programming languages you can explicitely specify the variable from which context you want to address, in Mathcad thats not possible.

But the solution is simple - just use different variable names.

One thing I haven't mentioned so far is that its inefficient to do the calculation of E.D inside the loop. As far as I can see its not dependent on anything which changes in the iteration so it should be precalculated outside of the loop, maybe even outside of the program.

Werner_E
25-Diamond I
(To:ptc-5020731)

BTW I could solve the problem with the vector and the scalar thing with that definition.

Clipboard05.jpg

Can't tell from the pic, but I hope the LHS of the last line is a F.2 and not a F[2

Werner_E
25-Diamond I
(To:ptc-5020731)

Now I see it! The picture you provided does not correspond to the file you posted!!

Your problem is that you wanted to do this:

27.04.png

Why is F[i not red in your screenshot?

Mathcad is absolutely right in saying "This value must be a vector".

Lets analyze it:

i=0 Vector F[0 is defined outside of the program as 3x1 vector (all 0), So F[i[0 yields 0 (Newton), K3 is a scalar (kg(s^2) and the calculations of the x[i..[j in the square brackets yield a result which is a scalar, too (m) - so the product is a scalar (in N) and the sum is a scalar (N) which is assigned F[1. Keep in mind: F[0 is a 3x1 vector, but F[1 is a scalar!!!

Now we come to i=1, and that line tries to evaluate (F[1)[2, but F[1 is a scalar and so you cannot access its third component and Mathcad says that it would have to be vector to able to do so - right!

So its not that vector behave differently inside of a program, but rather that you have to take care, that all F[i get a vector as value, not a scalar!

Would something like this do the job?

27.04_2.png

Werner Exinger escribió:

Now I see it! The picture you provided does not correspond to the file you posted!!

Your problem is that you wanted to do this:

27.04.png

Why is F[i not red in your screenshot?

Mathcad is absolutely right in saying "This value must be a vector".

Lets analyze it:

i=0 Vector F[0 is defined outside of the program as 3x1 vector (all 0), So F[i[0 yields 0 (Newton), K3 is a scalar (kg(s^2) and the calculations of the x[i..[j in the square brackets yield a result which is a scalar, too (m) - so the product is a scalar (in N) and the sum is a scalar (N) which is assigned F[1. Keep in mind: F[0 is a 3x1 vector, but F[1 is a scalar!!!

Now we come to i=1, and that line tries to evaluate (F[1)[2, but F[1 is a scalar and so you cannot access its third component and Mathcad says that it would have to be vector to able to do so - right!

So its not that vector behave differently inside of a program, but rather that you have to take care, that all F[i get a vector as value, not a scalar!

Would something like this do the job?

27.04_2.png

awesome, absolutely right

Werner_E
25-Diamond I
(To:ptc-5020731)

27.04_2.png

awesome, absolutely right

Really!?? That way the first two components of the 3x1 vectors Fi.. never change - as the WS is written they remain zero.

The code would first copy Fi over to Fi+1 making sure that way, that Fi+1 is a 3x1 vector. Then the third component of this nev vector is changed for the scalar calculated according to your pic.

Announcements

Top Tags