Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Finding the location of the maximum of a variable

Please login with a confirmed email address before reporting spam

Dear Comsol Community,

I am looking for a way to find the location of the maximum of a variable. Getting the maximum value is easy, but so far I have not been able to get the coordinates of the maximum, preferably without using Matlab. Does anyone know how to do this?

Best regards!

Christian

Edit: Removed typo in title

25 Replies Last Post Aug 29, 2016, 5:26 p.m. EDT
Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Feb 21, 2011, 6:03 a.m. EST
Hi,

in V4 you can get the maximum value with an operator (in 3.5 you needed to go through matlab) but I'm not sure how to get the coordinates too. Good point,does anybody else have a quick work-around ?

--
Good luck
Ivar
Hi, in V4 you can get the maximum value with an operator (in 3.5 you needed to go through matlab) but I'm not sure how to get the coordinates too. Good point,does anybody else have a quick work-around ? -- Good luck Ivar

Daniel Smith COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Feb 22, 2011, 10:22 a.m. EST
Hi, you can define a Maximum coupling operator, then pass in the independent variables as a second argument to query the location at which the maximum occurred. For example, say you solve for temperature, T, and you have a Maximum coupling operator, maxop1, then use:

maxop1(T,x) for the x-coordinate at which the maximum occurs
maxop1(T,y) for the y-coordinate at which the maximum occurs
Hi, you can define a Maximum coupling operator, then pass in the independent variables as a second argument to query the location at which the maximum occurred. For example, say you solve for temperature, T, and you have a Maximum coupling operator, maxop1, then use: maxop1(T,x) for the x-coordinate at which the maximum occurs maxop1(T,y) for the y-coordinate at which the maximum occurs

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Feb 22, 2011, 11:09 a.m. EST
Hi Daniel

that is an interesting news (where is that (will be?) documented, and are there other nice ones like that ;)

--
Good luck
Ivar
Hi Daniel that is an interesting news (where is that (will be?) documented, and are there other nice ones like that ;) -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Feb 22, 2011, 4:27 p.m. EST
Hi Daniel!

That is interesting indeed and worked well for me. Thank you! I guess it's time to update the documentation of Comsol ;o).

Best regards!

Christian
Hi Daniel! That is interesting indeed and worked well for me. Thank you! I guess it's time to update the documentation of Comsol ;o). Best regards! Christian

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Feb 23, 2011, 2:34 a.m. EST
Hi again,

well now I see the explanations too:

Help: About Scalar Model Couplings and Operators - About Maximum and Minimum Model Couplings

"You can give two arguments, and the returned value is the value of the second argument evaluated in the max/min of the first argument."

A simple example would better illustrate its use

As usual, everything IS written in the doc it's "just" to find it and to understand it in the right context ;)

--
Have fun COMSOLING
Ivar
Hi again, well now I see the explanations too: Help: About Scalar Model Couplings and Operators - About Maximum and Minimum Model Couplings "You can give two arguments, and the returned value is the value of the second argument evaluated in the max/min of the first argument." A simple example would better illustrate its use As usual, everything IS written in the doc it's "just" to find it and to understand it in the right context ;) -- Have fun COMSOLING Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Feb 23, 2011, 3:41 a.m. EST
Hi Ivar!

You're right, thanks for hinting me at the right page. This information is actually missing on the page you get to when you use the dynamic help (Help > Definitions > Model Couplings > Maximum). As you said, an example or something like the interface definition of the operator would be nice on that page.

Best!

Christian
Hi Ivar! You're right, thanks for hinting me at the right page. This information is actually missing on the page you get to when you use the dynamic help (Help > Definitions > Model Couplings > Maximum). As you said, an example or something like the interface definition of the operator would be nice on that page. Best! Christian

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 11, 2011, 8:14 p.m. EST
When I try this it tells me "Wrong number of inputs". How can one variable recieve two values from one operator anyways? Do we have to do something special? As a newbie, I couldn't be more disgusted with how poorly this amazing software is documented.
When I try this it tells me "Wrong number of inputs". How can one variable recieve two values from one operator anyways? Do we have to do something special? As a newbie, I couldn't be more disgusted with how poorly this amazing software is documented.

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 12, 2011, 3:55 a.m. EST
Hi

No the trick with the min/max call is that you receive only one value in return, either the maximum value of the variable name given, called with (u), or the x location of the maximum of the variable given called with (u,x), or the "y" value of the maximum of the variable given, called with (u,y) and so on.

You need 2 calls to get all x,y,and max_of_(u) (4 in 3D ...

By the way the v4.1 doc is far better than the earl 4.0

--
Good luck
Ivar
Hi No the trick with the min/max call is that you receive only one value in return, either the maximum value of the variable name given, called with (u), or the x location of the maximum of the variable given called with (u,x), or the "y" value of the maximum of the variable given, called with (u,y) and so on. You need 2 calls to get all x,y,and max_of_(u) (4 in 3D ... By the way the v4.1 doc is far better than the earl 4.0 -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 12, 2011, 4:08 a.m. EST
So why do I get a "Wrong number of inputs" error when I try putting two values into the max operator?
So why do I get a "Wrong number of inputs" error when I try putting two values into the max operator?

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 12, 2011, 5:02 a.m. EST
Hi

Good question, the firt must be a dependet variable or a locally defined variable and the second must be one of the spatial coordinates variables, x,y,z r ... (the second variable is a name not a value)

but the confusion could perhaps be from the

max(1,2) and min(2,3) mathematical functions, and the

maxop1(u,x) minop1(v,y) "coupling operators"

all in v4
--
Good luck
Ivar
Hi Good question, the firt must be a dependet variable or a locally defined variable and the second must be one of the spatial coordinates variables, x,y,z r ... (the second variable is a name not a value) but the confusion could perhaps be from the max(1,2) and min(2,3) mathematical functions, and the maxop1(u,x) minop1(v,y) "coupling operators" all in v4 -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 12:30 a.m. EDT
I am using Model 1 -> Definitions -> Model Couplings - > Maximum and I am trying maxop(c,t) where c is concentration (a dependent variable) and t is time (an independent variable). I have tried spacial coordinate x instead of time t, also, and it gives the error regardless. However, I am wondering if the problem is with how I am trying to evaluate the operator... I am going to Model 1 -> Definitions -> Variables and trying to evaluate it there... should I be doing it somewhere in the results instead? This is completely confusing to a newbie!
I am using Model 1 -> Definitions -> Model Couplings - > Maximum and I am trying maxop(c,t) where c is concentration (a dependent variable) and t is time (an independent variable). I have tried spacial coordinate x instead of time t, also, and it gives the error regardless. However, I am wondering if the problem is with how I am trying to evaluate the operator... I am going to Model 1 -> Definitions -> Variables and trying to evaluate it there... should I be doing it somewhere in the results instead? This is completely confusing to a newbie!

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 2:44 a.m. EDT
Hi

Then I suppose its because it expects a coordinate variable and not a time "t" variable.

To get a time (or a sweep) maximum you must post process, Derived Variables export the max value to a table, and in the table perform a max operation (table option at the bottom of the GUI, it took me some time to discover this one, just as for the table transpose possibilities. Its worth to explore thoughtfully all GUIs there are a lot of hidden gems in there

--
Good luck
Ivar
Hi Then I suppose its because it expects a coordinate variable and not a time "t" variable. To get a time (or a sweep) maximum you must post process, Derived Variables export the max value to a table, and in the table perform a max operation (table option at the bottom of the GUI, it took me some time to discover this one, just as for the table transpose possibilities. Its worth to explore thoughtfully all GUIs there are a lot of hidden gems in there -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 2:56 a.m. EDT
but, like i said, I tried a coordinate variable x and it still gave the error. can you tell me where and how you would evaluate the location of the maximum coordinate variable?
but, like i said, I tried a coordinate variable x and it still gave the error. can you tell me where and how you would evaluate the location of the maximum coordinate variable?

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 4:06 p.m. EDT
Hi

well for me it works in my v4.1, (if you are in 2D-axi it's "r" and "z" and not "x", "y", normally).
The only other way I know about is the to export to Matalb and do a matrix "max" search

--
Good luck
Ivar
Hi well for me it works in my v4.1, (if you are in 2D-axi it's "r" and "z" and not "x", "y", normally). The only other way I know about is the to export to Matalb and do a matrix "max" search -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 4:28 p.m. EDT
you have an amazing ability of answering the question without actually answering the question, ivar ;) but at least you resond, so i appreciate that. i really do.

~roman
you have an amazing ability of answering the question without actually answering the question, ivar ;) but at least you resond, so i appreciate that. i really do. ~roman

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 5:16 p.m. EDT
Hi

I s this better ;) If you do not a little search yourself, you will never learn !



--
Good luck
Ivar
Hi I s this better ;) If you do not a little search yourself, you will never learn ! -- Good luck Ivar


Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 5:26 p.m. EDT
yes, thank you for the wise advice, but trust me. i don't resort to this forum or to tech support unless i've spent days trying to figure out the problem on my own.

there is a difference in doing research on your own, or trying to find a needle in a hay stack. i've learned matlab on my own and they have everything documented such that i've used their support maybe 3 or 4 times in my life... whereas i've exceeded that with comsol in the first two weeks of use! and it is typical that you will wait a day for them to respond and they send you a generic response asking for more information, making it obvious that they didn't even bother to open the file that produces the error upon opening. once i waited over a week for them to respond, and in that week i figured out the problem on my own... so whats the point of contacting them?
yes, thank you for the wise advice, but trust me. i don't resort to this forum or to tech support unless i've spent days trying to figure out the problem on my own. there is a difference in doing research on your own, or trying to find a needle in a hay stack. i've learned matlab on my own and they have everything documented such that i've used their support maybe 3 or 4 times in my life... whereas i've exceeded that with comsol in the first two weeks of use! and it is typical that you will wait a day for them to respond and they send you a generic response asking for more information, making it obvious that they didn't even bother to open the file that produces the error upon opening. once i waited over a week for them to respond, and in that week i figured out the problem on my own... so whats the point of contacting them?

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 17, 2011, 5:42 p.m. EDT
Hi

when you have tough problems, going deep inside COMSOL then the people at "support" are very useful.

My advice: get hands on the new v4.2, when it arrives within a few month I suppose. The pdf doc (I expect a few thousand pages) need to be read at least 3 times to get most out of it, Then you will be occupied for a month without touching COMSOL ;).

I needed 4 years of physics studies, to cover about the full field of COMSOL, it's somewhat more complex than Matlab in that sens, the latter is for me a nice math tool, but math I started rather young, at least for +/-* ... ;)

Then if possible, take a few of the COMSOL 1 day training courses, that will give you a good start, and come to the conferences, follow the mini-courses, meet the other users, it worth it (ps I'm not COMSOL, just a fan of the tool, it's really nice to run Physics like that an be able to observe in details the phenomenas)

--
Good luck
Ivar
Hi when you have tough problems, going deep inside COMSOL then the people at "support" are very useful. My advice: get hands on the new v4.2, when it arrives within a few month I suppose. The pdf doc (I expect a few thousand pages) need to be read at least 3 times to get most out of it, Then you will be occupied for a month without touching COMSOL ;). I needed 4 years of physics studies, to cover about the full field of COMSOL, it's somewhat more complex than Matlab in that sens, the latter is for me a nice math tool, but math I started rather young, at least for +/-* ... ;) Then if possible, take a few of the COMSOL 1 day training courses, that will give you a good start, and come to the conferences, follow the mini-courses, meet the other users, it worth it (ps I'm not COMSOL, just a fan of the tool, it's really nice to run Physics like that an be able to observe in details the phenomenas) -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 2, 2012, 11:48 p.m. EDT

Hi, you can define a Maximum coupling operator, then pass in the independent variables as a second argument to query the location at which the maximum occurred. For example, say you solve for temperature, T, and you have a Maximum coupling operator, maxop1, then use:

maxop1(T,x) for the x-coordinate at which the maximum occurs
maxop1(T,y) for the y-coordinate at which the maximum occurs


When I use in Matlab
A=mphglobal(model, 'maxop1(emw.Ey,x)'); trying to get the X position of the maximum Ey, it always goes wrong.
However, I can use A=mphglobal(model, 'maxop1(emw.Ey)'); to get the maximum Ey

Can you show me how to use maxop1 to get the position of the maximum in Matlab?

Thanks
[QUOTE] Hi, you can define a Maximum coupling operator, then pass in the independent variables as a second argument to query the location at which the maximum occurred. For example, say you solve for temperature, T, and you have a Maximum coupling operator, maxop1, then use: maxop1(T,x) for the x-coordinate at which the maximum occurs maxop1(T,y) for the y-coordinate at which the maximum occurs [/QUOTE] When I use in Matlab A=mphglobal(model, 'maxop1(emw.Ey,x)'); trying to get the X position of the maximum Ey, it always goes wrong. However, I can use A=mphglobal(model, 'maxop1(emw.Ey)'); to get the maximum Ey Can you show me how to use maxop1 to get the position of the maximum in Matlab? Thanks

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 13, 2012, 5:36 p.m. EST
iomeg15,

You've probably long since moved on from this problem, but I think I understand your problem and how to fix it. When you generate the coupling maxop1, it's part of the problem definition, and not post-processing on the solution. If you generate the maxop1 and then try to use it without re-solving the solution it gives you the error you were seeing. If you update the solution though, then it works fine.

The others probably didn't catch this because having to update the solution was so obvious to them they probably didn't even think about needing to mention it.
iomeg15, You've probably long since moved on from this problem, but I think I understand your problem and how to fix it. When you generate the coupling maxop1, it's part of the problem definition, and not post-processing on the solution. If you generate the maxop1 and then try to use it without re-solving the solution it gives you the error you were seeing. If you update the solution though, then it works fine. The others probably didn't catch this because having to update the solution was so obvious to them they probably didn't even think about needing to mention it.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 13, 2012, 6:03 p.m. EST
I've definitely moved on, lol. But its still nice to know, and I'll give your suggestion a try if I need this in the future (which I am sure I will). Thanks!
I've definitely moved on, lol. But its still nice to know, and I'll give your suggestion a try if I need this in the future (which I am sure I will). Thanks!

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 14, 2012, 1:36 a.m. EST
Hi

indeed, one often forget to update the results data base, once one add some functions or variables.
But one should also think it over, not all changes can be updated, some require resolving fully, and that COMSOL cannot always "identify" so in doubt => clear the results and solve again

--
Good luck
Ivar
Hi indeed, one often forget to update the results data base, once one add some functions or variables. But one should also think it over, not all changes can be updated, some require resolving fully, and that COMSOL cannot always "identify" so in doubt => clear the results and solve again -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Apr 9, 2013, 2:53 p.m. EDT
Hi,

I have a trouble using this maximum finding operator when applied to a variable (Y-comp. displacement ) that is an output of a frequency domain analysis (Piezoelectric Devices physics). I am able to find the peak value of the displacement in a frequency response (let's say sweep range is 5...7 kHz) by using maxop1(pzd.uAmpY). Now, I want to know at which excitation frequency I get this peak displacement. If I use maxop1(pzd.uAmpY,freq) or maxop1(pzd.uAmpY, pzd.freq) I just get the maximum value of the sweep range (i.e. 7 kHz). Any ideas?


Cheers,
Rolanas
Hi, I have a trouble using this maximum finding operator when applied to a variable (Y-comp. displacement ) that is an output of a frequency domain analysis (Piezoelectric Devices physics). I am able to find the peak value of the displacement in a frequency response (let's say sweep range is 5...7 kHz) by using maxop1(pzd.uAmpY). Now, I want to know at which excitation frequency I get this peak displacement. If I use maxop1(pzd.uAmpY,freq) or maxop1(pzd.uAmpY, pzd.freq) I just get the maximum value of the sweep range (i.e. 7 kHz). Any ideas? Cheers, Rolanas

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Feb 24, 2016, 2:57 p.m. EST
very helpful answer.
i had just this problem and you solved it for me
Thank you.

I would just add that:
step 1: define maximum coupling operator under the component (comp #) definition
step 2: set the domain for which it will take the max
step 3: you can now call on the operator such that:
i. maxop1(T,x) for the x-coordinate at which the maximum occurs
ii. maxop1(T,y) for the y-coordinate at which the maximum occurs
iii. maxop1(T) for taking the max value of T itself

and don't forget to update the solution or run the solver after defining it, or else you will get an error message

very helpful answer. i had just this problem and you solved it for me Thank you. I would just add that: step 1: define maximum coupling operator under the component (comp #) definition step 2: set the domain for which it will take the max step 3: you can now call on the operator such that: i. maxop1(T,x) for the x-coordinate at which the maximum occurs ii. maxop1(T,y) for the y-coordinate at which the maximum occurs iii. maxop1(T) for taking the max value of T itself and don't forget to update the solution or run the solver after defining it, or else you will get an error message

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Aug 29, 2016, 5:26 p.m. EDT

Hi,

I have a trouble using this maximum finding operator when applied to a variable (Y-comp. displacement ) that is an output of a frequency domain analysis (Piezoelectric Devices physics). I am able to find the peak value of the displacement in a frequency response (let's say sweep range is 5...7 kHz) by using maxop1(pzd.uAmpY). Now, I want to know at which excitation frequency I get this peak displacement. If I use maxop1(pzd.uAmpY,freq) or maxop1(pzd.uAmpY, pzd.freq) I just get the maximum value of the sweep range (i.e. 7 kHz). Any ideas?


Cheers,
Rolanas


Hi Rolanas,
I have the exact same question: in a frequency domain analysis, I want to find the frequency at which a maximum happens (in my case maximum of a force, but the same issue). I can find the maximum force and that is very straightforward, but I also need the output to give me the frequency at which that happens too.
Have you found a solution or a way to achieve that?

Cheers,
Majd

[QUOTE] Hi, I have a trouble using this maximum finding operator when applied to a variable (Y-comp. displacement ) that is an output of a frequency domain analysis (Piezoelectric Devices physics). I am able to find the peak value of the displacement in a frequency response (let's say sweep range is 5...7 kHz) by using maxop1(pzd.uAmpY). Now, I want to know at which excitation frequency I get this peak displacement. If I use maxop1(pzd.uAmpY,freq) or maxop1(pzd.uAmpY, pzd.freq) I just get the maximum value of the sweep range (i.e. 7 kHz). Any ideas? Cheers, Rolanas [/QUOTE] Hi Rolanas, I have the exact same question: in a frequency domain analysis, I want to find the frequency at which a maximum happens (in my case maximum of a force, but the same issue). I can find the maximum force and that is very straightforward, but I also need the output to give me the frequency at which that happens too. Have you found a solution or a way to achieve that? Cheers, Majd

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.