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

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

How to iterate 2 variables in 2D plot?

Cornel
19-Tanzanite

How to iterate 2 variables in 2D plot?

Hi,

This should look like:

Cornel_5-1709804048998.png

 

Cornel_0-1709804275076.png


R3 is iterated from high value to low value...

Cornel_0-1709804347560.png

 

Cornel_0-1709803657110.png

Cornel_1-1709803775379.png

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Cornel)

Your function Vout is not using the "time" argument at all!

And your plot is not using R3 at the abscissa, so you get all constant values.

 

How should Vout change dependent on the time?

Maybe you have to define R3 as a function of t ....

 

In this example I assume a linear relationship between time and resistance R3

Werner_E_1-1709805241101.png

 

And here an exponential relationship between time and resistance R3

Werner_E_2-1709805587812.png

 

 

 

View solution in original post

10 REPLIES 10
Werner_E
25-Diamond I
(To:Cornel)

Your function Vout is not using the "time" argument at all!

And your plot is not using R3 at the abscissa, so you get all constant values.

 

How should Vout change dependent on the time?

Maybe you have to define R3 as a function of t ....

 

In this example I assume a linear relationship between time and resistance R3

Werner_E_1-1709805241101.png

 

And here an exponential relationship between time and resistance R3

Werner_E_2-1709805587812.png

 

 

 

Cornel
19-Tanzanite
(To:Werner_E)

All variables are depending on time...
Your first answer is good, I mean that with linear relationship between time and resistance R3.

I was thinking at 2 for loops, something like that:

Cornel_1-1709806112123.png
In this way I think if I will have 3, 4, or more variables that I need to iterate then maybe will be more easily to add another for loop, but I do not know exactly at this moment if this way is possible and a good way

Cornel_2-1709806124793.png

 

Werner_E
25-Diamond I
(To:Cornel)

Unless you want the two variables to 'iterate' independently from each other (which would result in a 3D surface plot)  you have to specify in which way one variable should relate to the other and this is done by defining functions.

A (IMHO bad) alternative would be to provide t and R3 as equal length (!!) vectors

Werner_E_0-1709807652795.png

 

Ranges are to be used in three occasions only:

1) To index the elements of vectors and matrices

2) as the independent variable in a 2D XY-plot (and there is only ONE independent variable in a 2D XY-plot)

3) in a program when you use a for-loop

Cornel
19-Tanzanite
(To:Werner_E)

I am not sure about vectorization operator: look at Vout...I did not put vectorization operator above Vout (as you did) and I can still see plot of Vout. Should be this case without vectorization operator? I am not sure if I understand correctly then when is needed to use vectorization operator and where is not needed to use vectorization operator...

Cornel_0-1709813326351.png

 

Werner_E
25-Diamond I
(To:Cornel)

When an operation is not defined (and/or does not make sense) for vectors, Prime applies implicit vectorization automatically.

This is the case in your expression where you divide the vector R3 by the vector R3+R1. There is no such thing like division defined for vectors, so Prime, instead of throwing an error (which would be justified) applies vectorization on its own.

IMHO it makes sense and is good style to apply vectorization manually whenever you want vectorization to be done and don't rely on Prime doing it automatically.

You also can avoid errors which may occur when you edit an expression and suddenly you get wrong results because implicit vectorization is not applied anymore.

Find out yourself why the results without and with vectorization are different in the following example: 😉

Werner_E_0-1709841935811.png

 

Here is another example:

Werner_E_1-1709842780123.png

 

Werner_E
25-Diamond I
(To:Cornel)

Concerning your function Test():

1) The function arguments R2 and time are useless because you redefine those variables immediately in the program, so they could be omitted

2) Your function has no explicit return value(s), so it would return just the last value calculated, which rendres all the loop senseles

3) The outer loop which uses time as iterator has no effect because variable time is nowhere used inside the loop.
Its like
Werner_E_0-1709808581379.png where R is assigned ten times the very same value (which is independent  from iterator "i")

4) The inner loop which uses R3 as iterator calculates at different value each time but the calculated value is stores every time in he very same variable Vout, overwriting the value which was assigned in the previous run.

5) The outer loop iterates 151 times, the inner one 94 times (last value would be 70 Ohm, not 68 Ohm because you use a stepwidth of 10 Ohm). So if it would work, how did you imagine that these 151*94 calculated values would be returned by your program? Which data structure? A 151x94 matrix? A 14194x1^vector (which order of the values)? Your program dos not even make an attempt to store those values and return them...

 

 

 

Cornel
19-Tanzanite
(To:Werner_E)

How to select first value and last value from below?

Cornel_2-1709809737550.png


I tried with Matrix index:

Cornel_3-1709809751394.png


I tried with Matrix row:

Cornel_4-1709809783587.png


I tried applying matrix column first then matrix row:

Cornel_5-1709809823092.png

Werner_E
25-Diamond I
(To:Cornel)


@Cornel wrote:

How to select first value and last value from below?

You don't!

What you do is a misuse of ranges! I wrote in an answer above where ranges should be used and this is non of the three cases.

Try to assign what you calculated to a variable ( X:=Vout(....) ) and you will see that you get an error.

What you see as result of your function Vout may look like a vector but it isn't. Its not even a range, in fact, its an invalid object.

A range, by the way is not a vector but rather kind of an implicit loop. So there are no individual values which you could address.

 

If your goal is to create a vector of voltage values using the function Vout, then you have to define "time" as a vector and call the function vectorized. Then result will be a vector which you can assign to a variable and where you can address the first and last value using the usual methods.

 

ttokoro
20-Turquoise
(To:Cornel)

image.pngimage.pngimage.png

image.png

Cornel
19-Tanzanite
(To:ttokoro)

Yes, but you did not showed how you did it. How you defined time. I followed Werner's suggestion and it works this way:

Cornel_0-1709884713603.png

But I am not sure if you did in this way or in other way.

Announcements

Top Tags