Error "Unrecognized function or variable 'x'."
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I got following error message:
>> ZUSTANDSRAUM
Unrecognized function or variable 'x'.
Error in ZUSTANDSRAUM (line 50)
rv = [x(4)-lv*x(6)+sind(x(3));
in following code:
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]
A = 0.3; %[Windanströmfläche]
Jv = 1.245; %[kgm^2]
Jh = 1.245; %[kgm^2]
r = 0.49;
%Eingangsgrößen
delta = 0; %[Lenkwinkel°]
Mav = 25.44; %Antriebsmoment [Nm]
Mah = 0;
Mbv = 0; %Bremsmoment [Nm]
Mbh = 0;
%Zeitbereich
tspan = [0 0.01 10];
%Anfangsbedingungen - Zustandsvektor
x0 =[0; % x(1): XV - KS X Richtung
0; % x(2): YV - KS Y Richtung
6; % x(3): PSI - Gierwinkel
50; % x(4): XV' - Geschwindigkeit in X Richtung [m/s]
10; % x(5): YV' - Geschwindigkeit in Y Richtung [m/s]
4; % x(6): PSI' - Giergeschwindigkeit
10; % x(7): pv' Winkelgeschwindigkeit Vorderes Rad
0; % x(8): ph' Winkelgeschwindigkeit hinteres Rad
56.9 ; % 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
% Vektorkombination
rv = [x(4)-lv*x(6)+sind(x(3));
x(5)+lv*x(6)*cosd(x(3));
0];
vrv = [cosd(x(3)+delta) sind(x(3)+delta) 0;
-sind(x(3)+delta) cosd(x(3)+delta) 0;
0 0 1]*rv;
rh = [x(4)-lh*x(6)+sind(x(3));
x(5)+lh*x(6)*cosd(x(3));
0];
hrh = [cosd(x(3)) sind(x(3)) 0;
-sind(x(3)) cosd(x(3)) 0;
0 0 1]*rh;
% Schlupfwerte
% Längsschlupf
sv = (r* x(7)-rv(1))/(max(abs(r*x(7)),abs(vrv(1))));
sh = (r* x(8)-rh(1))/(max(abs(r*x(8)),abs(hrh(1))));
% Schräglaufschlupf
av = -(vrv(2)/abs(x(7)));
ah = -(hrh(2)/abs(x(8)));
% Reifenkräfte
vFvxstat = cs*sv;
vFvystat = ca*av;
hFhxstat = cs*sh;
hFhystat = ca*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*A*x(4)*((x(4))^2+(x(5))^2)^0.5;
Fwy = 0.5*cw*pL*A*x(5)*(x(4)^2+(x(5))^2)^0.5;
[t,x] = ode45(@(t,x) odefcn(x, m, Fvy, Fhy, Fvx, Fhx, Fwx,Fwy, Mav, Mah, Mbv, Mbh, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat),tspan,x0);
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(12): hFh,y');
function dxdt = odefcn(x, m, Fvy, Fhy, Fvx, Fhx, Fwx,Fwy, Mav, Mah, Mbv, Mbh, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat)
dxdt = zeros(12,1);
dxdt(1) = x(1);
dxdt(2) = x(2);
dxdt(3) = x(3);
dxdt(4) = (m^-1)*(Fvx+Fhx-Fwx);
dxdt(5) = (m^-1)*(Fvy+Fhy-Fwy);
dxdt(6) = ((iz^-1)*lv*sin(delta)*x(9)+cos(delta)*x(10)-lh*x(12));
dxdt(7) = (1/Jv)*(Mav-Mbv*sign(x(7))-r*x(9));
dxdt(8) = (1/Jh)*(Mah-Mbh*sign(x(8))-r*x(11));
dxdt(9) = (cx*(r*x(7))/cs)*(vFvxstat-x(9));
dxdt(10)= (cy*(r*x(7))/ca)*(vFvystat-x(10));
dxdt(11)= (cx*(r*x(8))/cs)*(vFvxstat-x(11));
dxdt(12)= (cy*(r*x(8))/cs)*(vFvystat-x(12));
end
It would be nice if anyone on here could help me :)
Thanks in advance
1 Kommentar
Stephen23
am 16 Mai 2023
At the point you try to refer to x you have defined a variable named x0:
x0 =[0; % x(1): XV - KS X Richtung
... etc
but no variable named x. Did you mean to refer to x0 ?
Antworten (2)
Steven Lord
am 16 Mai 2023
The first line where you try to index into the variable x is the line where the error occurs. Where in your code prior to that line do you define that variable? Sure, you do define that variable on a later line but that doesn't tell MATLAB what value it should use on that line.
There's possibly also a problem with your call to the ODE solver. Let's take a look at that line and the one previous.
Fwy = 0.5*cw*pL*A*x(5)*(x(4)^2+(x(5))^2)^0.5;
[t,x] = ode45(@(t,x) odefcn(x, m, Fvy, Fhy, Fvx, Fhx, Fwx,Fwy, Mav, Mah, Mbv, Mbh, r,iz,delta,lv,lh,Jv,Jh,cs,ca,cy,cx,vFvxstat,vFvystat),tspan,x0);
If the variable x existed on that line where Fwy was defined, Fwy would be the value computed using the value of x as it exists when that line is executed. But you pass it into the ODE function, where x is also an input argument. If you're thinking that Fwy would be a function of x and referring to it in your ODE function would "automatically reevaluate it" for the value of x with which ode45 called your function, that's not how it works. Fwy is just going to be a number (or an array of numbers.)
I suspect you want to move all the code that defines expressions as a function of x into your odefcn function, so that they are evaluated at the value of x with which ode45 calls odefcn. This would greatly reduce the number of additional parameters your odefcn needs to accept and also avoid the "Unrecognized function or variable x" error (since you'll be evaluating those expressions on a line of code where x already exists.)
If for whatever reason that's not an option I suppose you could define those expressions as function handles and call those function handles inside your odefcn.
Fwy = @(x) 0.5*cw*pL*A*x(5)*(x(4)^2+(x(5))^2)^0.5;
But if any of cw, pL, or A are also functions of x the expressions can get long and complicated pretty quickly, as you'll need to call those functions with x as input. If all three of them were functions of x that return scalar values:
Fwy = @(x) 0.5*cw(x)*pL(x)*A(x)*x(5)*(x(4)^2+(x(5))^2)^0.5;
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!