Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Average of two plots

Please login with a confirmed email address before reporting spam

Hi

I have two line graphs (in 1d plot group) and i plotted same expresion with these two. Now I want to draw an average of these two graphs. Can any body tell me how I can do this?
At first I thought I can extract the data of these two plots so I will have X and Y values and by using matlab I can find the average of each two points. But since X of these two plots are not exactly the same I can not use this methode.(They don't have same steps, but they start and end in same points, and I don't know how to define the step size)

Any other suggestions?

Best
Bahar

6 Replies Last Post Dec 2, 2011, 12:45 p.m. EST
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 25, 2011, 9:19 a.m. EST
Hi

I would consider
a) a new Definitions variable Aaverage = aveop1(...) where aveop1 is an average operator defined in the Definition section
b) a pure postprocessing operation with a postprocessing average data set operation on one of the main or derived data sets

Solution A allows you to handle the variables as global variables for more complex calculations, solution b) does not give you that easily the freedom to combine postporcessing operators values

--
Good luck
Ivar
Hi I would consider a) a new Definitions variable Aaverage = aveop1(...) where aveop1 is an average operator defined in the Definition section b) a pure postprocessing operation with a postprocessing average data set operation on one of the main or derived data sets Solution A allows you to handle the variables as global variables for more complex calculations, solution b) does not give you that easily the freedom to combine postporcessing operators values -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 25, 2011, 9:48 a.m. EST
This is a solution that works in Matlab:
Since you already have extracted the Data of the plots in matlab you can use the matlab interp1 function to interpolate beween your points.
I assume you have 4 data variables in matlab.
x1 : xvaluesplot 1
x2 : xvaluesplot 2
y1: yvaluesplot 1
y2: yvaluesplot 2
so basically make a new vector of your variables like x=1:100 (x should run from the start to the end of your plot with a desired step size)

then calculate the average on every point of the new x seperation (you can use a finer seperation if you like)
y=(interp1(x1,y1,x) + interp1(x2,y2,x))/2

you can plot the average between your plots by plot(x,y)

Cheers
Robert
This is a solution that works in Matlab: Since you already have extracted the Data of the plots in matlab you can use the matlab interp1 function to interpolate beween your points. I assume you have 4 data variables in matlab. x1 : xvaluesplot 1 x2 : xvaluesplot 2 y1: yvaluesplot 1 y2: yvaluesplot 2 so basically make a new vector of your variables like x=1:100 (x should run from the start to the end of your plot with a desired step size) then calculate the average on every point of the new x seperation (you can use a finer seperation if you like) y=(interp1(x1,y1,x) + interp1(x2,y2,x))/2 you can plot the average between your plots by plot(x,y) Cheers Robert

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 25, 2011, 1:20 p.m. EST
Dear Ivar

I appreciate your reply. I understand the second solution but I have a question about the first one:

you mentioned that I should define this "Average = aveop1(...)" but by doing this, can I get an average in specific part of model?

I thought we can only use this if we want to average a parameter in a specific location.
However, in My case the parameters are "Electric fields" which have been measured along a cut line that I defined. So basically I have 2 Cut lines in different location of model and I have the electric field in the points along this line.
Do you think Is it possible to use the operator in this case?? Have you seen any examples which use this method?

Best
Bahar
Dear Ivar I appreciate your reply. I understand the second solution but I have a question about the first one: you mentioned that I should define this "Average = aveop1(...)" but by doing this, can I get an average in specific part of model? I thought we can only use this if we want to average a parameter in a specific location. However, in My case the parameters are "Electric fields" which have been measured along a cut line that I defined. So basically I have 2 Cut lines in different location of model and I have the electric field in the points along this line. Do you think Is it possible to use the operator in this case?? Have you seen any examples which use this method? Best Bahar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 25, 2011, 1:28 p.m. EST
Dear Robert

Thanks a lot for the reply and complete description. At first I tought since the steps of these two sets of X s (and hence f(x)) are different I can not use interpolation. But Now by reading your reply I think it should work! I am going to try that and post the results here :)

Best
Bahar
Dear Robert Thanks a lot for the reply and complete description. At first I tought since the steps of these two sets of X s (and hence f(x)) are different I can not use interpolation. But Now by reading your reply I think it should work! I am going to try that and post the results here :) Best Bahar

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 26, 2011, 3:39 a.m. EST
Hi Bahar

You are right that if you define a "Data Set Cut line" on the results, you cannot use that with an operator defined in the Definition node, as these only apply to existing Entities = domain, boundaries, ...

So either you make an internal bondary along a line or polyline ... in the geoemtry section (it's static) or you use the Postprocessing operators.

Unfortunately, from mxy knowledge, there is no "postprocessing variables" that can be defined in the Result section.

Perhaps COMSOL developers are hearing their ears sing now, hopefully, a suggestion for you here !! :)
A nice thing with COMSOL, often I see my whishes being exhausted in the new releases, provided the message passes.

So a "Result -Average" can (from my knowledge) only be dumped into a table, or a plot, and from there to a file, but NOT to a postprocessing variable.

What I have happened to do, is to dump the "cut-line data set" into a csv format file, then to read it in into an "int()" interpolation operator, and then use that for a second solving process, or furter data reduction, not the most handy way, but it works, from within the GUI.

The matlab approach gives you more freedom

--
Good luck
Ivar
Hi Bahar You are right that if you define a "Data Set Cut line" on the results, you cannot use that with an operator defined in the Definition node, as these only apply to existing Entities = domain, boundaries, ... So either you make an internal bondary along a line or polyline ... in the geoemtry section (it's static) or you use the Postprocessing operators. Unfortunately, from mxy knowledge, there is no "postprocessing variables" that can be defined in the Result section. Perhaps COMSOL developers are hearing their ears sing now, hopefully, a suggestion for you here !! :) A nice thing with COMSOL, often I see my whishes being exhausted in the new releases, provided the message passes. So a "Result -Average" can (from my knowledge) only be dumped into a table, or a plot, and from there to a file, but NOT to a postprocessing variable. What I have happened to do, is to dump the "cut-line data set" into a csv format file, then to read it in into an "int()" interpolation operator, and then use that for a second solving process, or furter data reduction, not the most handy way, but it works, from within the GUI. The matlab approach gives you more freedom -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 2, 2011, 12:45 p.m. EST
Dear Ivar

Thanks a lot for the complete answer.

I tried tried int() and also used Matlab. I also think in this Case Matlab have more freedom for processing Data. Hopefully Comsol people will think about a way to make it easier in next version!!

Best
Bahar
Dear Ivar Thanks a lot for the complete answer. I tried tried int() and also used Matlab. I also think in this Case Matlab have more freedom for processing Data. Hopefully Comsol people will think about a way to make it easier in next version!! Best Bahar

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.