how do I extend my function on the time axis by the function value of 0 ?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey guys i m new to matlab and i need help,
i generated a acceleration function for a simulink model. to make it work i have to increase the time axes to tFinal =10 sec but the acceleration must be 0 . the original function goes to t= 3.677s. Here the acceleration is alomost 0.
I thought i ll use a for loop to increase the time axis but it ist not working. can someone help me ?
%Fourierkoeffizienten eigenes Model
sk=[0.0 -0.00850314 0.0 0.00377249 0.0 -0.000182763 0.0 0.000029564 0.0000652721 0.000052017];
ck=[0.0240577 -0.0241291 0.0 -0.00210694 0.00208514 -0.00044844 0.000515647 -0.0000502304 0.000126237 -0.0000402195];
%Taktzeit in s
tT = 3/5*2*pi;
%Übertragungsfaktor phi = omega * t mit Omega in rad/s
omega = (1/tT)*2*pi;
%Zeitvektor für neues Signal
t_mHsl = linspace(0,tT,1000);
%Bestimmung des Beschleunigungsvorgabe
s_mhsl = zeros(length(1),length(t_mHsl));
v_mhsl = zeros(length(1),length(t_mHsl));
a_mhsl = zeros(length(1),length(t_mHsl));
for k = 0:length(sk)-1
%Wegvorgabe
p = 0;
s_mhsl = s_mhsl + (k^p*omega^p*(ck(k+1)*cos(p*pi/2+k*omega*t_mHsl)+sk(k+1)*sin(p*pi/2+k*omega*t_mHsl)));
%Beschleunigungsvorgabe
p = 1;
v_mhsl = v_mhsl + (k^p*omega^p*(ck(k+1)*cos(p*pi/2+k*omega*t_mHsl)+sk(k+1)*sin(p*pi/2+k*omega*t_mHsl)));
%Beschleunigungsvorgabe
p = 2;
a_mhsl = a_mhsl + (k^p*omega^p*(ck(k+1)*cos(p*pi/2+k*omega*t_mHsl)+sk(k+1)*sin(p*pi/2+k*omega*t_mHsl)));
end
% plot Beschl.Verl nach Erzeugung der Bewegungsvorgabe mit mHsl
mhsl.time = t_mHsl;
mhsl.a = a_mhsl;
% Beschl.
figure()
plot(mhsl.time,mhsl.a)
% Beschl.verlauf Errerfunktion verlängert von tT auf tFinal
t_mHsl_tFinal = linspace(0,tFinal,1000);
BeschlmHsl = zeros(length(1),length(t_mHsl_tFinal));
for i = 1:length(t_Final)
if t_mHsl_tFinal(i)<=t_mHsl(end) % endwert der Erregerfunktion für tT = 3/5 *2pi
BeschlmHsl(i) = a_mhsl(1,:);
else
BschlmHsl(i) =0;
end
end
figure()
plot(t_Final,BschlmHsl);
2 Kommentare
Jan
am 14 Mär. 2022
I've formatted your code. Please use the tools on top of the editing field. Thanks.
length(1) determines the number of elements of a scalar. It is easier to write "1" directly:
s_mhsl = zeros(1, numel(t_mHsl));
The command length is fragile, because it determines the longest dimension. This does, what you expect, for vectors. But for arrays it is a frequent cause of bugs. Use size(X, dim) or numel(X) instead.
What does this mean: "I thought i ll use a for loop to increase the time axis"?
The statement "but it ist not working" is less clear than a description of what you observe.
Antworten (1)
Jan
am 14 Mär. 2022
Bearbeitet: Jan
am 14 Mär. 2022
I'm not sure, what the purpose of the loop is, but this is not a loop at all:
for i = 1:length(t_Final)
If t_Final is 10, as you explain in the text, length(t_Final) is 1. Then the loop runs from 1 to 1.
Maybe you mean:
for i = 1:numel(t_mHsl_tFinal)
You have pre-allocated BeschlmHsl to zeros already. Then there is no reason to write 0 into the values again. Omit:
else
BschlmHsl(i) =0;
This cannot work:
BeschlmHsl(i) = a_mhsl(1,:);
There is a scalar on the left and a vector on the right.
3 Kommentare
Jan
am 15 Mär. 2022
I have mentioned, that I have formatted the code for you in the question. Please apply a proper formatting by your own to improve the readaility of the code. Thanks.
"i need the curve of the first figure and after t => 3.766 the acceleration curve must be 0" - please consider, that the reader cannot guess, which variable is meant. While this detail is obvious for you, it is a puzzle for the readers.
Maybe you mean:
t_mHsl_tFinal = linspace(0, tFinal, 1000);
BeschlmHsl = zeros(1, numel(t_mHsl_tFinal));
index = t_mHsl_tFinal < t_mHsl(end);
BeschlmHsl(index) = a_mhsl(1, 1000);
No loop needed.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!