Integral in matlab func

1 次查看(过去 30 天)
Dekel Mashiach
Dekel Mashiach 2022-7-8
编辑: Torsten 2022-7-9
hey; I'm getting this error and don't understand why. hope someone can help...
Persistent variable 'time_k1' is undefined on some execution paths. Function 'MATLAB Function5' (#61.404.411), line 24, column 17: "time_k1" Launch diagnostic report.
here is my func:
function [theta,V,Vx,Vy] = meas(Acc,gyro,time)
persistent theta_k1 wz_k1 Vx_k1 Vy_k1 Ax_k1 Ay_k1 time_k1
wz = gyro(3);
Ax = Acc(1);
Ay = Acc(2);
if time<0.01
theta_k1 = 0;
wz_k1 = 0;
Vx_k1 = 0;
Vy_k1 = 0;
Ax_k1 = 0;
Ay_k1 = 0;
time_k1 = time;
theta = 0;
V = 0;
Vx = 0;
Vy = 0;
else
% Integration for theta:
dt = time - time_k1;
theta = theta_k1 + dt*(wz + wz_k1)/2;
% Integration for V:
Vx = Vx_k1 + dt*(Ax + Ax_k1)/2;
Vy = Vy_k1 + dt*(Ay + Ay_k1)/2;
V = sqrt(Vx^2+Vy^2);
% Store for next step:
theta_k1 = theta;
wz_k1 = wz;
Vx_k1 = Vx;
Vy_k1 = Vy;
Ax_k1 = Ax;
Ay_k1 = Ay;
end

回答(1 个)

Torsten
Torsten 2022-7-8
编辑:Torsten 2022-7-8
You get this error because you didn't assign a value to the variable "time" when you called "meas" or a value >= 0.01.
If you give values to Acc, gyro and time (as I can see, Acc is an array of size at least 2 and gyro is an array of size at least 3) and then call "meas", you will have no problems.
  6 个评论
Dekel Mashiach
Dekel Mashiach 2022-7-9
I need to do the integrations in real time
Torsten
Torsten 2022-7-9
编辑:Torsten 2022-7-9
Change the line
if time<0.01
to
if time == 0
and call the function "meas" with time = 0 and arbitrary data for "Acc" and "gyro" before sensor data are being transfered to "meas".
Further in the "else" part, I think you will have to add the line
time_k1 = time
to store it for the next step.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Clocks and Timers 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by