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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Help me to learn Mathcad

ptc-1793698
8-Gravel

Help me to learn Mathcad

Hello,

 

I need some help in recalling and learning skills in order to do some research in material science.

This implies using Mathcad as a tool for interpreting electrical impedance data of semiconductor diodes.

I have been away from academic life for 10 years so I do not understand easily those mathcad files I used then.

 

Can you explain me how to integrate a function found on the basis of fitting?

 

Kristjan Laes

69 REPLIES 69

Thank you!

 

You saved my day!

 

Somehow the results quite significantly differs depending on the fit used for r1 and r2 data.

 

Cant figure out this for today.

 

In principle these are the steps what have to be repeated in loops.

 

How to put these into such loops?

How can I see on the graph how did the fitting functions of linterp and cspline did fit the data as a function?

I found the way. Thank you. 

This is what I do want if this was put in a recurrent loop.

cspline needs problably data collected more carefully.

Werner_E
25-Diamond I
(To:ptc-1793698)


@ptc-1793698 wrote:

I found the way. Thank you. 


Fine, you may also consider to use "pspline" and "lspline" instead of "cspline". All fit with cubic splines - the difference is just the handling of the ends but this affects the whole curve.

Thank you. lspline makes it far better.

 

But I have no idea yet how to use these calculations, splines and integrations with For and While cycles.

Could you give me a simple worksheet sample of the For loop sample where (for example) ten random values are summed up?

As an output please give

 

1) single result as a sum of these ten random values,

 

2) vector result where intermediate summation results are represented (vector with 10 elements).

 

I am totally stuck now, do not know how to move on.

 

Werner_E
25-Diamond I
(To:ptc-1793698)

Not sure but I guess this is what you are asking for and I hope it helps:

Werner_E_1-1647465203460.png

 

MC15 worksheet attached

 

Question about example 3:

 

ptc1793698_0-1647624464526.png

 

Here these results are in complex structure named loop_example3(10).

 

How to display the results random and sum as separate vectors (random) and (sum)?

Werner_E
25-Diamond I
(To:ptc-1793698)

The functions output is a 1x2 matrix whose elements are the vectors.

So you can assign the result to a 1x2 matrix of variables, like

 

[random  sum] := loop_example(10)

 

and then display the variables random and sum separately or use them for further calculations.

 

Or you assign the output to a single variable

 

Result : = loop_example(10)

 

which then is a 1x2 vector and display its elements one by one

 

Result[0,0 =.....                     Result[0,1 = ......

 

Here it might be better if the function is changed to return a 2 x 1 matrix (simply apply the transpose operator in the last program line), a vector, because now one index suffices to address the elements.

 

Werner_E_0-1647626529140.png

 

 

I see.

 

But why are these vectors not given as graphs? It allows to make a graph but the line is not visible in some reason.

 

ptc1793698_0-1647702629573.png

 

Werner_E
25-Diamond I
(To:ptc-1793698)

When plotting you have to use column vectors, not rows vectors. like you did.

Werner_E_0-1647704146303.png

The function I used returns column vectors "Zufall" and "Summe" (German for "random" and "sum")  and I used the transpose operator when I displayed them just to save space by showing the values in a horizontal row rather than a column.

 

When you plot a row vector over another the points will be displayed but they will not be connected by lines. The points are very tiny dots hard to be seen unless you chose a "Symbol" and an appropriate "Symbol weight" in the formatting options of the plot.

Werner_E_1-1647704701960.png

 

 

Thank you again!

 

Now I am going to try these insights for building the For cycle for my calculations. Let see, if something comes out.

ptc1793698_0-1647883652156.png

How to create a user-defined (it may be called sub-program, sub-routine etc.) function from these two lines:

 

ptc1793698_3-1647883959170.png

 

?

 

The content of my routine is already set, as show in my worksheets where I calculate  currents and voltages and I would like to re-use this verified calculation route inside of the For cycle.

 

Werner_E
25-Diamond I
(To:ptc-1793698)



How to create a user-defined (it may be called sub-program, sub-routine etc.) function from these two lines:

 

ptc1793698_3-1647883959170.png


You may use a global or a local subroutine to do the job.

This subroutine should add the created random number to the sum so far, therefore it has to know at least the last value in the sum vector but we may also provide the subroutine with the whole vector as argument.

The subroutine has to return the newly calculated sum and, because we also want to keep track of all random numbers used, the last created random number. So the routine may return a vector (or matrix) with those two scalars, but it may also return the full vectors of values.

Find attached a MC15 worksheet with a few different examples.

 

I tried to construct a loop for my calculations.

 

It gives me some errors, but I do not understand what is wrong.

Can you tell?

Werner_E
25-Diamond I
(To:ptc-1793698)

1) Move down the definition of r.1 etc. The error is because you define r.1 before r1r2R3 is defined.

 

2) Mathcad does not allow a vector of function to be created. You don't need it anyway - guess all thats needed is to remove the vector index when you define your interpolating functions. I think its no harm if they are redefined in each iteration step and have the same name.

 

3) When you defined r.2.func you used the formal argument "ii0" on the left, but "ii" on the right. Can be any name, but must be the same on both sides

 

4) When you defined R'.1 you forgot to type the dot after R'

 

5) when you define a function, the formal argument can't be a vector indexed variable. You can chose any name you like and call hat function with any other variable name if you like. So simply remove the vector index when you define your functions on both sides.

Example:

Werner_E_0-1648145143662.png

 

6) You define R1'.fu as a single variable but use it in the line below like a function when you define R'.1??
A further line below you again use the just defined variable R'.1 like a function?? Not sure if I corrected those error the way you intended it. Same problem with R2'.func.

 

7)  At the end of the program you use a matrix index "u" which nowhere is defined. No idea what you had in mind here. Should it be "i" instead??

 

In the attached sheet all errors with the exception of 7) should be corrected but maybe not the way you intended it.

 

Thank you for the answer!

 

There are still something missing and not quite right yet.

 

I have this R1'.fu because I need to calculate R'.1 as a value based on that function. The same for the R2'.func.

 

u stands there for rows in initial data.

 

 

Werner_E
25-Diamond I
(To:ptc-1793698)

It doesn't make sense to define a range variable like u in a program - you can't use it anyway.
Instead of a range variable which is kind of an implicit loop, you could use a for-loop.

 

Somehow all the steps that took me to results in first place should be included.

Is it possible at all to use such for loop in such calculations?

 

The program indicates different errors if I click on a name of the program loop.

For example: This value has to be vector.

 

Possible this is because of this u that is useless in that instance. 

If I take it out another error occurs: 

This array index is invalid for this array. 

and it indicates in 

 

ptc1793698_2-1648483599582.png

 

 

as if indice of i-1 is a wrong indication.

 

Obviously I do not understand the language that I should use.

 

 

Werner_E
25-Diamond I
(To:ptc-1793698)

When I open your sheet I see a different error than the one you describe:

Werner_E_0-1648486714013.png

So obviously the elements of matrix R'1 are not vectors and so you can't index them with index i.

To debug I inserted a return statement to see what R'1 looks like and as you can see it is a 2-element vector. The first element is the scalar 0 and the second is a 21x1 vector. I guess this is not as intended.

The reason is this:

Werner_E_1-1648487183442.png

So R'.1[0 is set to 0 by default and the elment with index 1 is the vector you assign.

Do you really need to keep all the vectors R'.1 for every iteration step? I don't think so but I can't be sure as I already lost track of your calculations and very demanding variable names long ago.

When I change the program so that R'.1 and R'.2 are overwritten en reused in every iteration step and change the call of function M accordingly, the next error pops up

Werner_E_2-1648487680762.png

I don't know what kind of data structure you have in mind for i.1, i.2 and V.1 !?
In every iteration step you call M 21 times and so each iteration step would produce 21 of those 3x1 vectors. Do you really intend to keep them all and is it this that your function should return?

 

BTW, the error you describe (index i-1) stems from the fact that your outer i-loop runs from 1 to n-1.
When you call your function with argument 1 this loop runs from 1 (down) to 0 and when i=0 the index i-1=-1 is invalid.
Why not let i run from i to n?

 

Werner_E
25-Diamond I
(To:Werner_E)

I guess/hope that the attached file may do what you intended.

 

Yes, this what you posted, is that what I expect. 

 

Just R1 and R2 need also to be extracted as are I1, I2 and V1.

 

In every iteration step M is calculated 21 times, and as a result vectors of I1 (21*1), I2 (21*1), V1 (21*1) are calculated.

 

I tried to extract R1 and R2, using indexes for rows e and a similarly as u was, but again something is not that right.

 

I am really grateful to you. These calculations are very important to me as these probably can fix my erroneous calculations made some ten years ago as then my concept of these calculations was not right. Hopefully now it is.

 

I copied results of these calculations as graphs and they truly look remarkable to me.

 

R1 and R2 are needed to be extracted as they form the basis for testing these results on the base of Ohm´s law in a circuit with a electromotive force.

 

 

Werner_E
25-Diamond I
(To:ptc-1793698)

Here is the modification to return R1 and R2 as well. Hope it makes sense.

I did 15 iterations and it seems the the resistors don't change significantly.

Werner_E_0-1648582525991.png

Note that because there are no initial values for the resistors, R1_iter[0 is not a vector but simply zero.

 

Werner_E
25-Diamond I
(To:Werner_E)

Here is a slight variant of the program, getting rid of R1_vec and R2_vec

 

I will put r1 and r2 as initial values for R1_vec and R2_vec, as such they are in the calculation approach, r1 and r2 coming from the modeling of impedance spectra as differential values dV/dI, as R1 and R1 should be V/I.

 

This is now what I really wanted. 

I shall now start to work with your last Template as first I am going to organize a bit my initial file and then go on to these other network schema put here before.

 

This is a great day for me! 

I started to study impedance as a student in 1998 in a field of electrochemical adsorption impedance of organic compounds in electrolyte solutions, and since then this method has been a true riddle for me to understand and really use in a way that makes sense in terms of physical models for me.

And now that is a doorway to this. To really start to get to the data needed for these physical models.

 

So that is it that I really appreciate your help that you provided to me here so much.

I hope this is now a huge step forward for everybody using impedance as a tool for studying materials and complex electrochemical systems.

But of course a work must be done yet. But this is a foundation.

 

Thank you!

 

ptc1793698_0-1648658182885.png

 

Werner_E
25-Diamond I
(To:ptc-1793698)

Glad that we finally arrived at something you can use to work with.

In the attached file I changed the for-loop for a while-loop which stops either at the maximum number of iterations or when the R1 and R2 values did not change more than a given threshold value in the last iteration step.
I could think of two possible ways for a measure of change

Werner_E_0-1648660944617.png

You may chose what you find most appropriate or use something else like the size of the difference vector.

When you are only interested in the last set of R1 and R2 values, you may also change the function so it will not keep all the intermediate steps but just the last two.

Good look for your projects!

This condition is condition is very useful to have in calculation loop.

 

We studied a bit the situation with the conversion as the number of loops enhances and found out that there is a certain limit. And possibly this is connected to the quality of initial data from fitting of impedance spectra.

ptc1793698_0-1648909832829.png

 

 

But also I found out that another question arises. The resistance calculated on R1,R2 and R3 should converge theoretically with that from device voltage  and current : V0/I0.

 

But they do not. 

ptc1793698_1-1648910015885.png

 If I insert a correction into the V0 and I0 something more overlapping appears, but not again.

ptc1793698_2-1648910124046.png

 

To estimate this offset, if this is a cause of the missing overlap between two curves of R: calculated and experimental, a system of two equations must be solved in the course of the looping program and applied in the next cycle:

ptc1793698_3-1648910630571.png

My colleague argued that this might be because of the device, but this still has to be found out.

 

Is it possible to incorporate this correction into the cycle?

Werner_E
25-Diamond I
(To:ptc-1793698)

> a system of two equations must be solved

Hmm, but what you show are 42 equations (u runs from 1 to 21) in just two variables A and B. Its not possible to solve that kind of a system but you could use a solve block with "minerr" to find "best fit" values A and B in each iteration.

Or do you expect A and B to be vectors with 21 elements, so actually they should read A[u and B[u ?

The latter could be achieved via a solve block wih "find" which is turned into a function and called from within the program.

But what would you like to do with the scalars or vectors A and B?

This is not yet clear to me which is better.

If these variables are due to the off-set of the measurement device there are two possibilities. Either this off-set is a constant in terms of A (volts) and B (amperes) and applies to every measurement condition, or it may have a kind of function, for example a linear function. 

My colleague argued that off-set may vary in different ranges of applied conditions, as a device switches between different electronic blocks if it leaves and enters different measurement ranges.

We didn't notice this before as something important. I was aware that there is such, but now it maybe turns out that it may interfere with these calculations.

 

I would like to see, what would that possible off-set look if it was calculated as a vector and with a find block.

If this turns out from this calculation that the values in that vector vary very little, the option with "minerr" to find a "best fit", would be enough.

 

 

Announcements

Top Tags