Azzera filtri
Azzera filtri

plotting variables within function

3 visualizzazioni (ultimi 30 giorni)
Marlon
Marlon il 12 Giu 2023
Commentato: Walter Roberson il 12 Giu 2023
Hi, so I'm trying to plot certain variables in my code, that are calculated in the function with state variables.
Plotting the state variables is working well but when I try to plot other Variables like Fwx over time I get an error saying
Unrecognized function or variable 'Fwx'.
Error in ZUSTANDSRAUM (line 63)
plot(t, Fwx); xlabel('t'); ylabel('Fwx'); % Plot von Fwx gegen t
Here's the code:
clear
close all;
clc;
%constant parameters:
m = 1700; % Fahrzeugmasse [kg]
lv = 1.2; % Abstand des Fahrzeugschwerpunkts zum Vorderachsmittelpunkt [m]
lh = 1.8; % Abstand des Fahrzeugschwerpunkts zum Hinterachsmittelpunkt [m]
iz = 500; % Trägheitsmoment um die Hochachse [kg*m^2]
cs = 10000; %Umfangssteifigkeit[N/°]
ca = 50000; %Schräglaufsteifigkeit[N/rad]
cx = 10000; %Reifenlängssteifigkeit
cy = 50000; %Seitetsteifigkeit
cw = 0.23; % CW Wert
pL = 1.2041; % Luftdichte [kg^m3]
Af = 0.3; %[Windanströmfläche]
Jv = 1.245; %[kgm^2]
Jh = 1.245; %[kgm^2]
r = 0.49; %m
%Eingangsgrößen
delta = deg2rad(0); %[Lenkwinkel°]
%Mav = 100; %Antriebsmoment [Nm]
Mah = 0;
Mbv = 0; %Bremsmoment [Nm]
Mbh = 0;
%Zeitbereich
tspan = [0 10];
%Anfangsbedingungen - Zustandsvektor
x =[0; % x(1): XV - KS X Richtung
0; % x(2): YV - KS Y Richtung
0; % x(3): PSI - Gierwinkel
70; % x(4): XV' - Geschwindigkeit in X Richtung [m/s]
0; % x(5): YV' - Geschwindigkeit in Y Richtung [m/s]
0; % x(6): PSI' - Giergeschwindigkeit
145.14; % x(7): pv' Winkelgeschwindigkeit Vorderes Rad
0; % x(8): ph' Winkelgeschwindigkeit hinteres Rad
204; % x(9): vFv,x Kraft Vorderreifen x-Richtung
0; % x(10): vFv,y Kraft Vorderreifen y-Richtung
0; % x(11): hFh,x Kraft Hinterreifen x-Richtung
0]; % x(12): hFh,y Kraft Hinterreifen y-Richtung
[t,x] = ode45(@(t,x) odefcn(m, lv, lh, iz, cs, ca, cx, cy, cw, pL, Af, Jv, Jh, r, delta, Mah, Mbv, Mbh,x,t),tspan,x);
subplot(4, 3, 1); plot(t, x(:,1)); xlabel('t'); ylabel('x(1): XV');
subplot(4, 3, 2); plot(t, x(:,2)); xlabel('t'); ylabel('x(2): YV');
subplot(4, 3, 3); plot(t, x(:,3)); xlabel('t'); ylabel('x(3): PSI');
subplot(4, 3, 4); plot(t, x(:,4)); xlabel('t'); ylabel('x(4): XV'' [m/s]');
subplot(4, 3, 5); plot(t, x(:,5)); xlabel('t'); ylabel('x(5): YV'' [m/s]');
subplot(4, 3, 6); plot(t, x(:,6)); xlabel('t'); ylabel('x(6): PSI'' - ');
subplot(4, 3, 7); plot(t, x(:,7)); xlabel('t'); ylabel('x(7): pv''');
subplot(4, 3, 8); plot(t, x(:,8)); xlabel('t'); ylabel('x(8): ph''');
subplot(4, 3, 9); plot(t, x(:,9)); xlabel('t'); ylabel('x(9): vFv,x');
subplot(4, 3, 10); plot(t, x(:,10)); xlabel('t'); ylabel('x(10): vFv,y');
subplot(4, 3, 11); plot(t, x(:,11)); xlabel('t'); ylabel('x(11): hFh,x');
subplot(4, 3, 12); plot(t, x(:,12)); xlabel('t'); ylabel('x(11): hFh,y');
Fwx =0.5*cw*pL*Af*x(:,4).*((x(:,4)).^2+(x(:,5)).^2).^0.5;
plot(t, Fwx); xlabel('t'); ylabel('Fwx'); % Plot von Fwx gegen t
function dxdt = odefcn(m, lv, lh, iz, cs, ca, cx, cy, cw, pL, Af, Jv, Jh, r, delta, Mah, Mbv, Mbh,x,t)
% Vektorkombination
Mav=15*t;
rv = [x(4)-lv*x(6)+sin(x(3));
x(5)+lv*x(6)*cos(x(3));
0];
vrv = [cos(x(3)+delta) sin(x(3)+delta) 0;
-sin(x(3)+delta) cos(x(3)+delta) 0;
0 0 1]*rv;
rh = [x(4)-lh*x(6)+sin(x(3));
x(5)+lh*x(6)*cos(x(3));
0];
hrh = [cos(x(3)) sin(x(3)) 0;
-sin(x(3)) cos(x(3)) 0;
0 0 1]*rh;
% Schlupfwerte
% Längsschlupf
sv = (r * x(7) - rv(1)) / (max(abs(r * x(7)), abs(vrv(1))) + 1e-10);
sh = (r * x(8) - rh(1)) / (max(abs(r * x(8)), abs(hrh(1))) + 1e-10);
% Schräglaufschlupf
av = -(vrv(2) / (abs(x(7)) + 1e-10));
ah = -(hrh(2) / (abs(x(8)) + 1e-10));
% Reifenkräfte
A=1.12;
C=0.625;
D=1;
n=0.6;
K=46;
d=5;
B = (K/d)^(1/n);
%vFvxstat = cs*sv;
%vFvystat = ca*av;
%hFhxstat = cs*sh;
%hFhystat = ca*ah;
vFvxstat =(m/4)*sign(sv).*(A.*(1-exp(-B*abs(sv)))+C*sv.^2-D*abs(sv));
vFvystat = (m/4)*sign(av).*(A.*(1-exp(-B*abs(av)))+C*av.^2-D*abs(av));
hFhxstat = (m/4)*sign(sh).*(A.*(1-exp(-B*abs(sh)))+C*sh.^2-D*abs(sh));
hFhystat = (m/4)*sign(ah).*(A.*(1-exp(-B*abs(ah)))+C*ah.^2-D*abs(ah));
% Radkraft Hinterachse
Fhx = cos(x(3))*x(11)-sin(x(3))*x(12);
Fhy = sin(x(3))*x(11)+cos(x(3))*x(12);
% Radkraft Vorderachse
Fvx = cos(x(3) + delta)*x(9)-sin(x(3) + delta)*x(10);
Fvy = sin(x(3) + delta)*x(9)+cos(x(3) + delta)*x(10);
% Luftwiderstand
Fwx =0.5*cw*pL*Af*x(4)*((x(4))^2+(x(5))^2)^0.5;
Fwy =0.5*cw*pL*Af*x(5)*(x(4)^2+(x(5))^2)^0.5;
dxdt = zeros(12,1);
dxdt(1) = x(4); %Geschwindigkeit x-Richtung
dxdt(2) = x(5); %Geschwindigkeit y-Richtung
dxdt(3) = x(6); %Gierbeschleunigung
dxdt(4) = (m^-1)*(Fvx+Fhx-Fwx); %Beschleunigung x-Richtung
dxdt(5) = (m^-1)*(Fvy+Fhy-Fwy); %Beschleunigung y-Richtung
dxdt(6) = ((iz^-1)*lv*sin(delta)*x(9)+cos(delta)*x(10)-lh*x(12)); %Gierbeschleunigung
dxdt(7) = (1/Jv)*(Mav-Mbv*sign(x(7))-r*x(9)); %Radwinkelgeschwindigkeit VA
dxdt(8) = (1/Jh)*(Mah-Mbh*sign(x(8))-r*x(11));%Radwinkelgeschwindigkeit HA
dxdt(9) = (cx*(r*x(7))/cs)*(vFvxstat-x(9)); %Radkraft VA X-Richtung
dxdt(10)= (cy*(r*x(7))/ca)*(vFvystat-x(10)); %Radkraft VA Y-Richtung
dxdt(11)= (cx*(r*x(8))/cs)*(hFhxstat-x(11)); %Radkraft VA X-Richtung
dxdt(12)= (cy*(r*x(8))/ca)*(hFhystat-x(12)); %Radkraft VA Y-Richtung
end

Risposte (1)

Torsten
Torsten il 12 Giu 2023
Recalculate Fwx from the solution (see above).
  4 Commenti
Marlon
Marlon il 12 Giu 2023
Is there a way to pass variables outside the Function, that I don't have every calculation of the Variables I want to plot two times in the code?
Walter Roberson
Walter Roberson il 12 Giu 2023
Yes, there are ways. However, in the context of ode functions, it is almost always the wrong thing to do.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by