Clear Filters
Clear Filters

I need help using embedded matlab functions for an iterative learning controller

2 views (last 30 days)
I am working with a learning controller for a motor for a periodic signal. I want to store the followerror on each time sample during the first period, in a vector. Then I want to output these values the next period, and also rewrite them with the new followerror.
currently I am using:
function y = fcn(u,t)
%#codegen
d=round(t);
z=zeros(1,99999);
y=z(1,d+1);
z(1,d+1)=u;
t is a time counter that resets every period
but this doesn't seem to work, it keeps showing zero... Can anyone help me?

Answers (1)

Kaustubha Govind
Kaustubha Govind on 19 Mar 2013
You are using the variable 'z' as a state, but note that the line:
z=zeros(1,99999);
is executed at every time-step, so 'z' is reset to all zeros every time. You might want to define 'z' as a persistent variable instead:
function y = fcn(u,t)
%#codegen
persistent z;
d=round(t);
if isempty(z)
z=zeros(1,99999);
end
y=z(1,d+1);
z(1,d+1)=u;
PS: You might also want to look into whether you are assigning 'z' correctly, you are using y=z(1,d+1), but z(1,d+1)=u; only on the next line. Did you mean to say y=z(1,d)?

Categories

Find more on Deep Learning with GPU Coder in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!