Implementation of Iterative Learning Control in Matlab/Simulink - matlab

I am working on the development of an Iterative Learning Controller for a simple transfer function.
The iterations are controlled by the external matlab loop.
But the error e(k) (k is trial number) is not updating ... as the trials increases.
Please detect the error I've commited.
Thanks and Regards.

You might have solved the problem. But as the question is still open, I would like to add something here.
First of all, you might want to check the usage of "memory" block. "The Memory block holds and delays its input by one major integration time step." The reason why the error wasn't updating is that the output of your plant produced was the same in each iteration(you defined external loop). The memory block only delayed one step of your U(K), not the whole iteration.
You might want to store the error of each iteration to workspace, and use it for the next iteration.

The memory should be a vector with a lenght of the single iteration. Not just single value. Delay block can store multiple past samples.
This guy did probably what you were looking for: https://github.com/arthurrichards77/iterative-learning-control

Related

How to avoid using the To Workspace block

I ran the profiler on my Simulink model and realized that the "To Workspace" block is using 20% of the total simulation time. Because this model is ran more than one time, I'm looking for a way to increase its performance.
Hence, is there an alternate solution to using the "To Workspace" block that would increase my model global performance?
Yes, you can use Signal Logging. The various approaches to logging simulation results are discussed in the documentation under Export Simulation Data. Finally, see also View Simulation Results for alternative approaches. My personal recommendation would be signal logging or a To File block.
According to my general understanding of memory management, reserving a fixed memory block takes less time than expanding it in each timestep. So, it might be useful to limit the number of data points to be logged, that the memory space reserved for your data set will not be dynamically increased at every timestep. Of course this would be only valid if you would know the number of data points and therefore number of steps in simulation prior to start of the simulation, which can be achieved by a fixed step size solver (if applicable by your simulation system setuo). Thus pre-allocation of the workspace array might save you some time in terms of not reaching the memory management system at each timestep.

Link Matlab to Window Application

How compatible are Matlab and .EXE files? Is it possible to make win32 APIs send messages to matlab and for matlab to read them in real time?
More precisely can I make Matlab to receive and handle messages from another application at real time?
When I am handling such messages, I also have a concern with the type of loop I would have to use in Matlab. Is an infinite for/while loop a good practice?
for example
while(infinite loop)
if (received message)
do something
end
end
Note, above is algorithm only, not intended as code.
The first part of your question: the Matlab Engine seems to be what you are after.
The second part of your question: in many coding standards, it's often recommended to avoid infinite loops. The problem with infinite loops is, well, that they may never end :) It's all too easy to incorrectly or incompletely code the exit condition(s), causing your loop to never end and the program's execution to stall. This sort of bug can pop up in unit testing (often-failing exit condition), or only after the first batch of your customers start complaining about your program crashing (not-so-often failing exit condition). These (and many other) pitfalls of infinite loops are often avoidable by
translating the infinite loop to a finite one
setting a maximum on the amount of iterations
using a whole different paradigm altogether.
With IPC, where part of the program is listening to messages from other parts of the program or other programs alltogether, the last option is best. Using an event based approach prevents an infinite loop. MATLAB supports this in the form of events and listeners. This is part of OOP in MATLAB, so you'd have to have followed OOP already, or convert everything you have to OOP in order to use it.

Why does Matlab run faster after a script is "warmed up"?

I have noticed that the first time I run a script, it takes considerably more time than the second and third time1. The "warm-up" is mentioned in this question without an explanation.
Why does the code run faster after it is "warmed up"?
I don't clear all between calls2, but the input parameters change for every function call. Does anyone know why this is?
1. I have my license locally, so it's not a problem related to license checking.
2. Actually, the behavior doesn't change if I clear all.
One reason why it would run faster after the first time is that many things are initialized once, and their results are cached and reused the next time. For example in the M-side, variables can be defined as persistent in functions that can be locked. This can also occur on the MEX-side of things.
In addition many dependencies are loaded after the first time and remain so in memory to be re-used. This include M-functions, OOP classes, Java classes, MEX-functions, and so on. This applies to both builtin and user-defined ones.
For example issue the following command before and after running your script for the first run, then compare:
[M,X,C] = inmem('-completenames')
Note that clear all does not necessarily clear all of the above, not to mention locked functions...
Finally let us not forget the role of the accelerator. Instead of interpreting the M-code every time a function is invoked, it gets compiled into machine code instructions during runtime. JIT compilation occurs only for the first invocation, so ideally the efficiency of running object code the following times will overcome the overhead of re-interpreting the program every time it runs.
Matlab is interpreted. If you don't warm up the code, you will be losing a lot of time due to interpretation instead of the actual algorithm. This can skew results of timings significantly.
Running the code at least once will enable Matlab to actually compile appropriate code segments.
Besides Matlab-specific reasons like JIT-compilation, modern CPUs have large caches, branch predictors, and so on. Warming these up is an issue for benchmarking even in assembly language.
Also, more importantly, modern CPUs usually idle at low clock speed, and only jump to full speed after several milliseconds of sustained load.
Intel's Turbo feature gets even more funky: when power and thermal limits allow, the CPU can run faster than its sustainable max frequency. So the first ~20 seconds to 1 minute of your benchmark may run faster than the rest of it, if you aren't careful to control for these factors.
Another issue not mensioned by Amro and Marc is memory (pre)allocation.
If your script does not pre-allocate its memory it's first run would be very slow due to memory allocation. Once it completed its first iteration all memory is allocated, so consecutive invokations of the script would be more efficient.
An illustrative example
for ii = 1:1000
vec(ii) = ii; %// vec grows inside loop the first time this code is executed only
end

RTL simulation vs Delta cycle simulation

Could some one please elaborate on ​"RTL simulation is faster than delta-cycle simulation but can not be used in all situations"?
I don't know what Delta cycle simulation
In a Verilog simulation delta-cycles are those used by the simulator to calculate the next value.
When entering a combinatorial section the simulator will use many delta cycles to resolve the dependencies required for the answer. If you have combinatorial loops not broken up with a flip-flop the simulator may get stuck constantly reiterating the loop trying to resolve the dependencies, which is impossible due to the loop. If you get a simulation which just hangs this is often the cause.
The non-blocking assignment (<=) makes use of delta-cycles, by resolving the right hand side values (potentially many delta-cycles), then a delta cycle later assign these to the left hand side.
a<=b;
b<=a;
In the simple case above b and a are copied to temporary location (think next_a next_b). Then a delta cycle later a is assigned to next_a and b to next_b.
There are other ZERO-Time constructs which do not use a delta cycles often used in Test benches for modelling. I have no experience of these hopefully another answer could describe there use.

Share variables between two MATLAB m-files executing separately

I'd like to know if there is something like pointers in MATLAB.
I have two matlab routines which execute simulataneously (that is they are lunched together and run on the same machine, which is, therefore, synchronized in terms of time-stamps).
The first routine, A, has a parfor loop where a data vector is continuously updated. The second routine, B, needs to get access to a specific (but variable in time) row of the vector (of routine A) and do further calculation.
My first guess is to print the updating rows of routine A in a txt file and then get access to it in B when necessary. However, this will result in a large amount of waste time.
I know that this could be related to parallel jobs and scheduler but I dont know how to implement it.
Thank you for any help, guesses or solutions.
Probably this code will help you, if i got correctly your problem. I am waiting for further comments.
Best wishes!