For loop over slider values then save the corresponding output of another variable

Diaa shared this question 6 months ago
Answered

If I have an animating drawing where the the input is given by a slider whose name is RackDisplacement.

Without using the animation, I would like to :

1- save the values of a variable called SteeringAngle for the corresponding values of RackDisplacement in a spreadsheet using for loop

2- create another column in the spreadsheet with the title of OutputAngle which is given by the equation acotd( cotd(SteeringAngle) + 3/11)

3- plot a graph between RackDisplacement and OutputAngle

Best Answer
photo

Thanks mathmagic, I was looking for this command.

This makes the transfer to the spreadsheet clearer. (Attachment 4B_RackWithAngle.ggb)

.

But (1):

The performance goes down significantly. Especially when moving the slider "RackDisplacement" with the keyboard. I don't know exactly why, but I suspect that locAPf resp. locBPf is recalculated too often.

.

In the version "4C_RackWithAngle" I defined the two objects as free objects with loc..Pf(x)=x and in the update of loc..P (points list) I insert with SetValue() the function LineGraph(..) in loc..Pf. With this now the performance is normal.

But (2):

the script is executed with every movement from point A resp. B, so that my assumption above (recalculated too often) is disproved. Why the performance is nevertheless better remains one of the many GGB mysteries in the area of "when is what recalculated". If anyone in our galaxy really knows, a description would be very useful.

Comments (36)

photo
2

1) Please post your .ggb file


2) Maybe https://wiki.geogebra.org/e... is interesting

photo
1

Please, find the attached file.

I am very new to GeoGebra, so I don't figure out how to do it with scripting.

photo
1

Thanks mathmagic, I was looking for this command.

This makes the transfer to the spreadsheet clearer. (Attachment 4B_RackWithAngle.ggb)

.

But (1):

The performance goes down significantly. Especially when moving the slider "RackDisplacement" with the keyboard. I don't know exactly why, but I suspect that locAPf resp. locBPf is recalculated too often.

.

In the version "4C_RackWithAngle" I defined the two objects as free objects with loc..Pf(x)=x and in the update of loc..P (points list) I insert with SetValue() the function LineGraph(..) in loc..Pf. With this now the performance is normal.

But (2):

the script is executed with every movement from point A resp. B, so that my assumption above (recalculated too often) is disproved. Why the performance is nevertheless better remains one of the many GGB mysteries in the area of "when is what recalculated". If anyone in our galaxy really knows, a description would be very useful.

photo
1

the post above is on the wrong place.

maybe one of the administrators can delete him (include this post).

thanks.

photo
photo
1

Try right-click on Steering Angle -> Record to Spreadsheet. Then move the slider with the arrow keys


/w8YG8Zco+0fJwAAAABJRU5ErkJggg==

photo
1

Thanks for your answer but I have some inquiries:

1- Can I do this programmatically using the script instead of this manual approach?

2- I would like to create a third column in the spreadsheet with the title of OutputAngle which to be given by the equation acotd( cotd(SteeringAngle) + 3/11)

3- Plot a graph between RackDisplacement and OutputAngle

photo
1

1) Not easily. You can animate the slider to semi-automate it


2) Yes, create another variable and trace that


3) C2=(A2,B2) then copy and paste down

photo
photo
2

alternative

without manual intervention and if the list in the spreadsheet is not needed

Solution approach with command Locus() (much easier)

photo
1

Your answer is really beautiful, and I have some inquiries:

1- How did you change the layout by having another axis panel below the original one? I always get them side-by-side.

2- How did you put labels for the new axes (degree and RackDsiplacement)?

3- What is the meaning of / ° in this definition?

/Gq+BVqvOVrI2AEjIARmCAEOiLmCSrT2RgBI2AEjEALBEzMLcDxLSNgBIzAZCBgYp4M1F2mETACRqAFAibmFuD4lhEwAkZgMhAwMU8G6i7TCBgBI9ACgf8DZQcrHxcR1pwAAAAASUVORK5CYII=

photo
2

1)

Use the script command SetPerspective()

example: SetPerspective("A(G/D)S") or SetPerspective("(AGS)/D")

2)

927226fc186d8864df9ee39d895bf9b0


3)

The character ° is interpreted by GGB as Operation/Factor.

° corresponds: <number>*(pi/180)

(try in ggb: pi/180==°)

/° corresponds to: <number>*(1/(pi/180)) or <number>*180/pi (reciprocal of °)

so that the degree-value gets the displayed value (example: 30°/° == 30)

This is what I used to use the Y-axis as a pseudo degree


4) addon

It is possible to convert the locus into a Point-List (First(<locus>, Length(<locus>))) and this in a PolyLine

With this you can fill (approximately only to 2 decimal places exactly) the spreadsheet as shown in the appendix.

(Note: since recently there is a command that allows a conversion of the points list into a function. Unfortunately I can't remember the name and I can't find a documentation either[I mean something else then Fit...()]).

Addendum to "function instead of polyline": not shure but maybe this (I think it is similar but need not script tricks)

photo
1

I apologize for not noticing your reply before now. I didn't get an email.

Your second code is really rich for me and I am sure it is as informative as the first one.

I will get back in case I don't get something.

Thanks

photo
1

a conversion of the points list into a function. 

https://wiki.geogebra.org/e... ?

photo
2

Thanks mathmagic, I was looking for this command.

This makes the transfer to the spreadsheet clearer. (Attachment 4B_RackWithAngle.ggb)

.

But (1):

The performance goes down significantly. Especially when moving the slider "RackDisplacement" with the keyboard. I don't know exactly why, but I suspect that locAPf resp. locBPf is recalculated too often.

.

In the version "4C_RackWithAngle" I defined the two objects as free objects with loc..Pf(x)=x and in the update of loc..P (points list) I insert with SetValue() the function LineGraph(..) in loc..Pf. With this now the performance is normal.

But (2):

the script is executed with every movement from point A resp. B, so that my assumption above (recalculated too often) is disproved. Why the performance is nevertheless better remains one of the many GGB mysteries in the area of "when is what recalculated". If anyone in our galaxy really knows, a description would be very useful.

photo
1

Many thanks for your patience.

Coming across GGB after years of using MATLAB shows me how superior a software can be in terms of code optimizations and compilation time regardless the user input, GGB really needs to thoroughly work on this global sluggish performance issue.

Anyway, I have one question please: why do you use RemoveUndefined and First commands? Have you encountered such cases where you needed them?

photo
1

I assume you are referring to the command:

  • locAP = Unique(RemoveUndefined(First(locA, Length(locA))))

"First(locA, Length(locA))" is a syntax convention within GGB to convert a locus into a dot list.

In this case, "RemoveUndefined" is actually not necessary.

photo
1

Do you have any idea why writing =LocusLeftPf(A4) in a cell of the spreadsheet doesn't evaluate what I wrote and just put the literal text LocusLeftPf(A4) as it is?

Also writing in any cell 3*A4 gets me 3A4 instead of doing multiplication.

However, writing =LocusLeftPf(A4) in the input bar/field gives me the expected calculated result.

photo
1

Why do you think I can answer this question without your file?

Please send your file with the problem and describe what should be achieved.

photo
1

I thought it might be a known beginner mistake. Anyway, please have a look at the spreadsheet columns here https://www.geogebra.org/cl...

I need to apply your concept of version 4C in your previous comment.

photo
1

Change in the preferences for spreadsheet: AlgebraDescription=Value (instead of Description)

C2 has a parenthesis error so GGB has created a text instead of the formula. Additionally: the cell reference is wrong.

The max-value of "Rackdisplacement" must be increased minimally (RackStroke+0.001) so that the last value (75) is still displayed in the spreadsheet (rounding error of Locus(), round down)

The creation of "LocusLeftPf(x)=x" may not be defined in the script, but only once in the algebra view (Performance)

photo
1

Many thanks for your consideration. Regarding your comments:

"The max-value of "Rackdisplacement" must be increased minimally (RackStroke+0.001) so that the last value (75) is still displayed in the spreadsheet (rounding error, round down)"

I am sorry I don't quite understand what you mean. I would be grateful if you showed me it in the file.

"The creation of "LocusLeftPf(x)=x" may not be defined in the script, but only once in the algebra view (Performance)"

I believe I defined it in the On-Update script of LocusLeftAnglePoints, so what is wrong/missing?

Thanks in advance for your patience!

photo
2

First Delete the cell then type in C2

= LocusRightPf(A2)

-------------------------------------

28c4129a0947cff267d2aea4531251b2

.

-----------------------------------------------------------------------------------------------

.

4dbbb2bd3d6b0790c8bef207aeab247e

.

-------------------------------------------------------------------------------------------------------

photo
1

I greatly appreciate your effort and time you took to make me understand a lot of valuable stuff. Out of curiosity, LocusLeftPf(x) is drawn in Graphics canvas, is there a way to move it to Graphics 2 without the need to delete it then SetActiveView("D"), then draw it again?

photo
2

you are welcome

.

08cd9aa38ddb86fb0bc89367ac9711b0

photo
1

Addition, so that the vehicle can also drive and hold a straight lane.

Note: now with adjust ScrubRadius. Maybe another parameter is better.

photo
1

scripting for ScrubRadius corrected

photo
1

It looks my automotive theory course topic is interesting to you XD. Actually, if you need to help me with making the wheels straight ahead at RackDisplacement=0, I have an angle I add manually in the definition of RightWheelShaftCenter (auxiliary orange dot beside the right wheel).

/H8iw9LGdgqDEwAAAABJRU5ErkJggg==

I need this angle to be be automatically calculated and added whenever the steering mechanism geometry (RackLength, ArmLength, etc) changes in order to have zero RightSteeringAngle and LeftSteeringAngle.

Theoretically, speaking, the scrub radius is largely affected by the suspension design not the steering mechanism geometry.

photo
1

I have another issue if you still have enough patience :smile: When following your code here of version 5A, I got an error of undefined LocusLeftPf after writing the required script in the onUpdate of LocusLeftAnglePoints because of this line


SetValue(LocusLeftPf, LineGraph(x(LocusLeftAnglePoints), y(LocusLeftAnglePoints)))
. I bypassed this error by defining LocusLeftPf(x)=3 in the input bar to just create the function, which worked for me. Was the error expected and I did the right thing?

Or was there a missing step from my side?

photo
2

quote: "I bypassed this error by defining LocusLeftPf(x)=3 in the input bar to just create the function, which worked for me. Was the error expected and I did the right thing?"

Yes, I think it is OK.

to be sure I would have to have the file)

----------------------------

regarding "RightWheelShaftCenter" I will get back to you later.

photo
1

Update: for the manual angle I mentioned in my previous reply, it is called ArmAngle in my file so you don't have to create another one. I copy its value when RackDisplacement=0 and paste it into the definition of RightWheelShaftCenter.

photo
1

Never mind! I just realize it was easy to do by creating a free object ArmAngleStraight then set its value to that of ArmAngle in the OnClick script of Reset button.

Then replace the manual value I write by this free object in the definition of RightWheelShaftCenter.Your codes are really rich and helped me a lot.

photo
2

this is a good, and simple solution.

Probably you should add the reset-script with stopping the animation.

Possibly, but rather unlikely, the reset-script must still contain the command UpdateConstruction().

If you now start to play with the variables, there is a risk that many objects will end up in the Undefined state and that you will no longer be able to get the application running again. If you now additionally end the application with Save, then your whole work starts again from the beginning.

You should definitely create a save with a own name as a backup copy. Do this often.

So for me the topic "ArmAngle" is done for now. Contact me if you need further support for this or any other topic. (please do not forget to provide the current state of the application)

photo
1

Regarding your version 5C attached in your previous comment, would you please suggest how to write the OnUpdate script of LocusRightAnglePoints in javascript (not GGB script) since I got a syntax error (invalid or unexpected token) when writing

ggbApplet.evalCommand("SetValue(LocusRightPf, LineGraph(x(LocusRightAnglePoints), y(LocusRightAnglePoints)))")

Also, I need to use the exists() operator before this line to declare a function LocusRightPf(x)=0 only if not existent to avoid the error of undefined object.

photo
1

1.) sorry, For some time I do not suport Javascipt (very very rarely)

2.) without your current status (ggb file or link) I can not provide support.

3.) Without a description of the problem that does not anticipate the solution to the problem, I do not want to provide support.

The question is why LocusRigthPf(x) no longer exists and (if this is indeed the case) whether this is a GGB bug for which there is a workaround or whether an alternative must be found for LineGraph().

-------------------------

Unwilling and exceptionally to your specific questions:

The only way to execute the LineGraph() command in JS is with ggbApplet.evalCommand(''). This is the same as if it was entered in the input line.

the JS command you are looking for is (probably)

if(! ggbApplet.exits('LocusRightPf')) {ggbApplet.evalCommand('LocusRightPF(x)=x')} <-- not tested

photo
1

I am innocent :) because I think you probably overlooked the first line of my last reply where I said

"Regarding your version 5C attached in your previous comment,"

--------------------

Anyway, you can find in the attached file here that I copied and pasted your codes in the OnUpdate script of LocusRightAnglePoints,

if(! ggbApplet.exits('LocusRightPf')) {ggbApplet.evalCommand('LocusRightPF(x)=x')}
ggbApplet.evalCommand('SetValue(LocusRightPf, LineGraph(x(LocusRightAnglePoints), y(LocusRightAnglePoints)))')


and it didn't work since I got the following error

/DnSLCkLizA03e0usZRciXDXuP3KNnD173qPOMSo5Nxqcblf72nLtLlwpOkAAAAAElFTkSuQmCC


Generally speaking, If possible, I need to automate:

1- the creation of LocusRightPf if not defined, then set its value to the LineGraph as done before

2- and the filling of the spreadsheet columns

photo
1

Innocent or guilty is not the question.

Because I will process your questions only if I have also received your latest version. In other words, if you want to send your questions into the void, you can do so by not attaching a file.

---------------------------------------------------------------

with your file now I can see the following picture:

25b5d82503b5be01a3534df44a8cf932

There are 2 problems:

  • The comment lines do not comply with the JS syntax (//)
  • Typo in line 6 (created by my self in my last post)

note: without the file i would never have found this out

----------------------------------------------

I cannot judge what the above is supposed to be good for. Since the causes (preconditions) are not described.

I don"t enjoy that at all. I don't intend to do proofreading work and certainly not without understanding the meaning over all (and JS anyway only in justified exceptional cases)

photo
1

I think this tread is too long see also here.

Please open a new tread with reference to this tread.

photo
© 2021 International GeoGebra Institute