Using the basic motor (DCPM) motor equation ...electrical and mechanical which have differential equations. I can solve the differential equations in a solve block. I want to create a sum squared error between the measured data and the model, and then using minerr to estimate the motor parameters. The measured data is Ia vs time and rotation speed vs time. But I cannot have two odesolve in one solve block. Can you help please... thanks.
Solved! Go to Solution.
Thanks Werner, this is awesome, much appreciated.
Thanks Weiner and Terry for your prompt responses on this weekend. Now I understand the method 🙏.
Have a great weekend.
I have no experience in your field of work but it looks to me that you could parametrize the first solve block and use it in the second.
But without seeing what you actually are trying to solve and looking at your worksheet I can't say much more.
I assume you are using Prime and not real (legacy) Mathcad. In that case you also should say which version you use to avoid that you were presented a solution sheet in a version you can't open (in case you are not using the latest version of Prime).
You might be able to treat them as components of a system of differential equations,
See, for example:
Stuart
(Mathcad rime 10) Attached is a pdf of what I intended to do. I have the basic motor equations and I get the armature current and rotational speed vs time for a step voltage input. Now I have the experimental data as well. So I wanted to derive the sum squared error between the experimental data and the simulated data (same time steps) and minimize the error for Ia and omega so as to predict the Ra, La, Kt, Kb given a set of constraints for these variables so the solver would iterate and give me the best "fit" values.
Parametrize the solve block, making "Sol_sim" a function of the variable inputs: Sol_sim(R.a_actual, L.a_actual, ....) := odesolve(....)
and then I.A(....):=.... etc.
You could use a second solve block with "minerr" to find the optimal parameters.
If you need more help you would need to to post the worksheet itself and the data.
You also should take advantage of Primes ability to work with units!
BTW, as you use t.sim (first line you should replace "i" by "j" ) just for the plots, it would not be necessary to make it a vector. A range fits pretty well for plotting - its one of the things ranges could and should be used for.
For the fit you can use the vector of time values from your measured data and the corresponding vectors you get when you use this vector as argument for the functions derived by the solve block.
I am still having issues, can you please take a look .. thanks
I updated the file, but still does not work
You have to use names used in the function definition also in the equations of the solve block.
In your previous answer you wrote that you want to "predict the Ra, La, Kt, Kb".
Now you also have added J as function argument.
Does this mean that you also want to predict the optimal value of J?
This means that only B, tau and Vin are constants which should not be altered - is this correct?
You are correct, I want to estimate Ra, La, Kt, Kb and J. Yes, B, Tloss are constants with a constant step voltage input.
@SA_1844545 wrote:
You are correct, I want to estimate Ra, La, Kt, Kb and J. Yes, B, Tloss are constants with a constant step voltage input.
But in the meantime I looked further in your sheet and noticed that you also tried to fit B as well (you requested B to be in the range from 0 to 4).!?? That would mean we have six parameters to play with. I had set up the sheet that way.
But if B really should be constant 1 you should be able to remove it from the calculations yourself.
The fit found seems to be pretty good. The results are quite sensitive with respect to the guesses and guess values all zero as you tried are unusable. Values in the center of the ranges you requested give a pretty good fit. Using the ...actual values you provided may give an barely noticeable better fit (according to the SSE calculated).
The estimated value for B is about 3.97 here.
BTW, I added the constraint B=1 in the last solve block and Prime really respected it. The fit for I was slightly better, but the fit for omega was worse with B=1. (only noticeable by looking at the SSE, the plots look fine either way).
It has to be noticed that the additional constraints are soft constraints. "minerr" tries to minimize all the errors, this concerns the given equations but also the additional constraints.
You may notice that Prime had not respected and exceeded the ranges for Kt and J.
If this is a problem you can 'weight' the corresponding constraint by multiplying the values with something larger than 1. However, doing so may come at the expense of other constraints or the quality of the fit.
This is something you have to experiment with.
I had luck with replacing 0.1 < J < 0 with 100 * 0.1 < 100 * J < 0 and using 30 * 3 < 30*Kt < 0.
I got
and the fit for I was slightly better, but the one for omega was a bit worse.
Using 20 instead of 30 in the Kt-constraint made things much worse.
EDIT: Crazy! Using a guess value of 0.005 for La instead of 0.0005 gives us a solution where all values are within their requested ranges (no 'weighting' needed) and provide a pretty good fit. I called it crazy, because 0.005 is an 'invalid' guess as it exceeds the limit 0.001 you had set. Nonetheless the resulting value 0.0008.. for La is pretty within this limit and all other values respect the constraints as well.
So it may be worth playing around with the guesses to find the best fit (according to the SSEs).
Prime 10 sheet attached
Hi,
I used a different technique to get excellent fits to the measured data using the "genfit()" function.
Also changed the first solve block with the ODE solver. No need for a second solve block.
See enclosed file:
Cheers
Terry
As I understood the very same parameter values should be used for both functions - not one set of estimated parameters for I and another one for omega.
Depends on whether you want accuracy. The matches are exact. In a real DC motor there are other losses present that are not accounted for in the simplified equations particularly in the mechanical system equation so it is not surprising you get two different parameter sets.
Cheers
Terry
Thank you Werner, Thank you Terry, I really appreciate you both taking the time to review and provide solutions as this is the first time I did an optimization/parameter extraction in Mathcad, and I have a couple more projects to complete. I am going to review your solution in detail. This gives me a good foundation of the methodology 🙏
Correct, one set of parameters to meet both the Ia and omega .. thanks Werner
@SA_1844545 wrote:
Correct, one set of parameters to meet both the Ia and omega .. thanks Werner
It appears that Terry is more at ease with the two different sets of parameters. 🙂
When I saw his solution and played with his sheet I was asking myself whether it would be possible at all to use "genfit" to fit the very same set of parameters for the two different functions. My first attempt (simply fitting the sum of the two functions) failed terribly. But now I seem to have found a suitable way, even though its a bit tricky. The trick is to shift one of the functions horizontally and use the "if" function to mask the function outside the relevant range.
The calculation took half of an eternity to finish, because with this approach, the odesolve block is called 251 times (once for each value in the vector t.measure) at each iteration step performed by “genfit.” In the sheet I posted before I managed that the solve block was only called once per iteration step done by "minerr" but I could not find a similar solution for "genfit" because genfit refuses to work with vector-valued functions.
Nonetheless the fit was finally equally good as the one in the first sheet I posted. Drawback of this method apart from the calculation time needed is that you can't provide additional constraints like 1>Kb>0. So I had to experiment a bit with the guesses until I managed to get a result where all of your constraints were met (vector of all 1's at the end).
Modification of Terry's Prime 10 sheet attached
Thanks Werner, this is awesome, much appreciated.
Thanks Weiner and Terry for your prompt responses on this weekend. Now I understand the method 🙏.
Have a great weekend.
