Numerical Integration cumtrapz in matlab mfiles compaired to Simulink Integrator block?

1 view (last 30 days)
Hallo all,
I am not sure why this code is not working:
if true
%%PID Test file
% Start the script
clear
clc
kp = 180;
kI = 3200;
kD = 1;
start_time = 0;
step_time = 1;
end_time = 2;
Ts = 0.1;% Sample Time
step_value = 1;
initial_value = 0;
Fn = 50;
W = 2*pi*Fn;
Phase = 0;
Amp = 1;
steps = 1;
if steps
[t,y] = step_fun(start_time,step_time,end_time,Ts,step_value,initial_value);
sim('PID_Test_sim.mdl')
else
t = start_time:Ts:end_time;
y = sin(W*t);
sim('PID_Test_sim.mdl')
end
x_min = start_time;
x_max = end_time;
y_min = initial_value - 1;
y_max = step_value + 1;
figure();
subplot(2,1,1);
stairs(t,y);grid on;hold on; stairs(ScopeData.time,ScopeData.signals(1,2).values,'--r');hold off;%y([y_min y_max]);xlim([x_min x_max]);
subplot(2,1,2);
plot(t,y);grid on;hold on; plot(ScopeData.time,ScopeData.signals(1,2).values,'--r');hold off;%y([y_min y_max]);xlim([x_min x_max]);
Input = y;
time = t;
dt = diff(time);
D = [0,diff(Input.*kD) ./ dt]';
I = [cumtrapz(time,(Input.*kI))]';
%I = [0;(I(1:end-1)+I(2))];
I_test = I;
I_test(I_test>0) = (I(find(I>0,1,'first'))) + I(I>0);
I_test = [0,I_test(1:end-1)];
P = [Input*kp]';
Compare = ScopeData1;
figure();
subplot(3,1,1);
stairs(time,P);grid on;hold on; stairs(Compare.time,Compare.signals(1,1).values,'--r');hold off
subplot(3,1,2);
plot(time,I);grid on;hold on; plot(Compare.time,Compare.signals(1,2).values,'--r');hold off
subplot(3,1,3);
plot(time,D);grid on;hold on; plot(Compare.time,Compare.signals(1,3).values,'--r');hold off
Test = [I ScopeData1.signals(1,2).values time'];
[time,Output] = PID_fun(kp,kI,kD,Input,time,ScopeData1);
figure();
subplot(2,1,1);
stairs(t,y);grid on;hold on; stairs(ScopeData.time,ScopeData.signals(1,2).values,'--r');hold off;
subplot(2,1,2);
plot(time,Output);grid on;hold on; plot(ScopeData.time,ScopeData.signals(1,1).values,'--r');hold off;
Test = [I ScopeData1.signals(1,2).values I_test P ScopeData1.signals(1,1).values D ScopeData1.signals(1,3).values time' Output ScopeData.signals(1,1).values];
end
It is a very simple code that generate a step signal (in step_fun) and sfterword run the simulation mdl which is a PID controller with step function as input. and then i am running my PID function which is explained later by variables (P,I,D) and in the integration i am becomming different results between simulation and mfile? can you help me with this? ther results are:
if true
% code
% My integration results of the input signal when kI = 3200;
mfile simulation
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
160 0
480 320
800 640
1120 960
1440 1280
1760 1600
2080
1920 2400 2240 2720 2560 3040 2880 3360 3200 end here is some figures with the results:
Best Regards and thx in advance

Accepted Answer

Aubai
Aubai on 7 Feb 2014
0 Answers i though this is an basic question!! I had to answer myself: cumtrapz accumulate the results till the studied point. yet simulink accmulate the results till n-1 of the studied point. that is why i have modified the cuntrapz to cumtrapzt which will get the expeceted results. (Please Matlab try to add this clearly in the documentation)

More Answers (0)

Categories

Find more on Simulink 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!