C_R

1960 Reputation

19 Badges

6 years, 51 days

MaplePrimes Activity


These are replies submitted by C_R

@jediknight
This assignment is causing the problem wx:=solve(GainQ1=Top_of_Q1);

Each time you execute this line

     Top_of_Q1:=eval(maximize(GainQ1),wx=0.1..2);wx:=solve(GainQ1=Top_of_Q1);

GainQ1 changes. This changes Top_of_Q1 which changes wx. And so on...

Here is an alternative way Maple offers without assignments (i.e. `:=`), where you get an equation for wx(Top_of_Q1). Using this way you will hardly run into errors related to unintened assignments.

 

GainQ1 = 8*wx^2*(m-1)/(Pi^2*sqrt((m*wx^2-1)^2+Q1^2*wx^2*(wx^2-1)^2*(m-1)^2))

GainQ1 = 8*wx^2*(m-1)/(Pi^2*((m*wx^2-1)^2+Q1^2*wx^2*(wx^2-1)^2*(m-1)^2)^(1/2))

(1)

params := m = 6.3, Q1 = 1

m = 6.3, Q1 = 1

(2)

subs(params, GainQ1 = 8*wx^2*(m-1)/(Pi^2*((m*wx^2-1)^2+Q1^2*wx^2*(wx^2-1)^2*(m-1)^2)^(1/2)))

GainQ1 = 4.296018185*wx^2/((6.3*wx^2-1)^2+28.09*wx^2*(wx^2-1)^2)^(1/2)

(3)

plot(rhs(GainQ1 = 4.296018185*wx^2/((6.3*wx^2-1)^2+28.09*wx^2*(wx^2-1)^2)^(1/2)), wx = .2 .. 2, axes = boxed, thickness = 5, axis[1] = [mode = log], title = `Gain (w, Q, m)`)

 

Top_of_Q1 = maximize(rhs(GainQ1 = 4.296018185*wx^2/((6.3*wx^2-1)^2+28.09*wx^2*(wx^2-1)^2)^(1/2)))

Top_of_Q1 = .8281966930

(4)

subs(GainQ1 = Top_of_Q1, Top_of_Q1 = .8281966930, GainQ1 = 4.296018185*wx^2/((6.3*wx^2-1)^2+28.09*wx^2*(wx^2-1)^2)^(1/2)); solve(%, {wx})

{wx = -.8945713660-0.1334178082e-4*I}, {wx = .8945713660+0.1334178082e-4*I}, {wx = -.8945713660+0.1334178082e-4*I}, {wx = .8945713660-0.1334178082e-4*I}

(5)

For numerical reasons, the solutions are complex. Alternatively you can use fsolve but an "error term" has to be added to get a real valued solution

subs(GainQ1 = Top_of_Q1, Top_of_Q1 = .8281966930, GainQ1 = 4.296018185*wx^2/((6.3*wx^2-1)^2+28.09*wx^2*(wx^2-1)^2)^(1/2)); fsolve((rhs-lhs)(%)+0.1e-5, {wx = .2 .. 2})

{wx = .8938364815}

(6)

Or use:

Optimization[Maximize](rhs(GainQ1 = 4.296018185*wx^2/((6.3*wx^2-1)^2+28.09*wx^2*(wx^2-1)^2)^(1/2)))

[.828196692670812418, [wx = HFloat(0.8945713660947568)]]

(7)

NULL

 

Download WO_assignment.mw

@Joe Riel 

Including a code optimization option in the custom component template (and making the intermediate variables disappear in the parameter pane) would be much more convenient than my separate Maple worksheet or inserting the commands into the template. Also, every user of a custom component could benefit from code optimization without having to learn the required Maple commands.

I would definitely use it!

@acer

If its by design, I will make the type specification my default. In my example the value of la is smaller than 0.5 which leads to errors of 50% and more (red C code vs Maple code in green).

By the way, codegen also has an undocumented design “feature”. When codegen is used in my original worksheet instead of CodeGeneration, the parameters defined as outputs appear as “passed by reference” in the C code. This works also with other names than outputs. Very handy. With CodeGeneration[C], “passed by referenced” must defined manually. (Since codegen is deprecated I will not ask, how codegen magically predicts what the outputs are.)

Thank you for the clarification!

@acer 

Sorry for the confusion.  

I found the type double as a synonym for float[8] here: help(type, float[8]). If the parser replaces it with float[8] I don't see a problem (if...).

The comment is more of a personal note. I meant: Type definition to double was added to get all parameters defined as double in the C code output of a subsequent call to CodeGeneration.

What is still not clear to me:

In the equation list there is one equation (t54 = 1 + 200 * la) that can be interpreted as integer relation. This could be the reason why CodeGeneration defines “t54” and “la” as int.

What I just tried: When this equation is inserted into an evalf call, "t54" and "la" are defined as double in the C code (without requiring your solution).

codegen neither requires your solution nor evalf workarounds (which make the code less clean).

With these observations I would ask now: Is this (assumed) interpretation of such equations a design feature or something unintended (i.e., something that should be reported)?

@acer 

A perfect oneliner leaving even space for a "why comment"

codegen[makeproc](TheList, parameters = map(`::`, [proc_params], double));# double required because CodeGeneration[C] sometimes puts int instead of double.

Since codegen[C] did not show this effect: Do you think its worth bringing this difference to the attention of Maplesoft?

Thank you!

 

Visual Studio Community 2017 and 2019 replace the Express Edition.

A bit hidden when following the download link in help(Setup, Compiler):

On the MS download pages: There is no download link to Visual Studio Community 2017. Installing another 2017 download, will list the Community package under the Available tab in the Visual Studio Installer.  

Desktop development with C++ must be installed additionally (available under the Workloads tab of the package).

These are the new paths:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsx86_amd64.bat

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsx86_amd64.bat

@tomleslie 

Before asking I consulted indets and type\structure (where indets points to). In the later I could not find an appropriate match. That’s where I stopped.

I should have gone to the type help page (referenced by type\structure) and browsed through the more than 250 (!!!) types. I'm not sure I would have recognized suffix on the first go, since this word had no meaning in my life (so far).

That’s why I consider nm`s answer the closest in my reach.

The help page of type\structure is quite intimidating for someone who only knows "normal" types of programming languages. This rich choice of types reminds me of the concept of function, which is used much more narrowly in engineering than in mathematics. Acer has made some efforts to introduce me to this level of abstraction.

The type command and its uses is another good example that Maple is not a programming language.

Thank you for taking your time!

@acer

I was not aware of that (implication by the choice of a command). Now it makes sense.

The alternative for the (undocumented?) `if` call is indeed intersting.

Thank you for these insights!

@Carl Love @nm @tomleslie

These are all excellent answers!

It's difficult to pick a winner.

nm answer uses commands that can be found in the help system.

That suffixed can be used as a command without "type" suprises. From the help system I can deduce at most a use like this

            select(X -> `if`(type(X, 'suffixed(t, integer)'), true, false), lst)

(I assume that Carls &under is required for other structures than sets.)

Thanks allot!!!

@Joe Riel @Thomas Richard 

Here is an update what I could figure out:

  • ExternalCCodeGen I was refering to is actually not an old Maple command as I assumed, but a procedure written by the author of the video. A call to CodeGeneration (to create a C code string) and to FileTools[Text][WriteFile] (to write the string to a file) could be used for a replacement of ExternalCCodeGen.
  • Much simpler to save is Thomas solution.
  • Saving is also possible with the External C/Library Block App and the option “Text Area”. In the text area: copy&paste Maple Output of C code -> optionally modify -> save file.

Using C code in MapleSim with the External C/Library Block App requires customizing the generated code to definitions (#define) and parameters accepted by the App. Knowledge of C is needed for that.

@chriskiwi 

I meant: You could upload screen shots that we can see how a small palette looks like.

I use a PC with monitors of different resolutions, which requires adjusting the scaling options. Maple in native 4K resolution works, but menus are just too small on a notebook (but a real joy on a large screen)

attached samples (green arrow for upload) are also helpfull.

@Joe Riel 

I did that (with the Custom Component App) before I generated the C code with the same list of equations.

That worked well and matched the kinematics of a MapleSim model.

Now I want to test (verify) C code with MapleSim before using it outside MapleSim. A test simliar to what is demonstrated here https://www.youtube.com/watch?v=uaQh_bXcIGA.

Let's see how far I get with the solution of Thomas. I will send an update.

@Thomas Richard 

In other words: string can be the value of output but if the value of output is a string its a filename.

I missed the two meanings of the string string in the paragraph of the help page. At least it is not obvious.

Thank you!

@tomleslie 

Now it makes sense.

The equation label (2) in my example labels an output (the return value xa,xc of cat). Referring in a subsequent statement to the same equation label evaluates the output to its value (which are equations). This explains why no eval is needed when two statements are used.

This also explains why no additional eval is needed in nested statements when an output or a return value is already evaluated, which is the case for most commands.

Thanks for the help in identifying this subtle difference.

First 25 26 27 28 29 30 31 Last Page 27 of 36