Unrecognized function or variable 'ys'
2 views (last 30 days)
Show older comments
u= 0.9 ; % friction coefficient
Fz = 4500 ; % Newtons
As = 0 ; % friction discount factor
Cx = 80000 ; % lateral stiffness
Cy = 47275 ; % tire longitudinal
alp = (-1:0.001:1) ; % Slip Ratio (User Chooses)
s1= (-100:.1:100) ;
s= s1/100;
Fx = zeros(1,length(s)) ;
Fy = zeros(1,length(s)) ;
H = zeros(1,length(s)) ;
for i=1:length(s)
H(i) = (sqrt(((Cx.*s(i)).^2) + (Cy.*tan(alp(i))).^2))./(u*Fz.*(1+s(i)));
end
for i=1:length(H)
if H(i)<0.5
Fx(i) = Cx.*(s(i)./(1+s(i)));
else, if H(i)>=0.5
Fx(i) = Cx*(s(i)/(1+s(i)))*((1/H(i))-(1/(4*(H(i)^2))));
end
end
if H(i)<0.5
Fy(i) = Cy.*((tan(alp(i)))./(1+s(i)));
else, if H(i)>=0.5
Fy(i) = Cy.*((tan(alp(i)))./(1+s(i))).*((1./H(i))-(1./(4.*(H(i).^2))));
end
end
end
tiledlayout(4,1)
nexttile
plot (alp,Fy)
title ('Slip angle vs Long. force')
xlim([-1 1])
ylim([-5000 5000])
nexttile
plot (s,-Fx)
title ('Slip ratio vs Lat. force')
xlim([-1 1])
ylim([-5000 5000])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Magic Formula
Sv=0;
Sh=0;
B= 30;
xm= -200;
D = 10000;
y= zeros(1,length(s));
Y= zeros(1,length(s));
for p=1:length (s)
C = (2/pi)*(asin(ys/D));
ys= D*sin((pi/2)*C);
E= ((B*xm - tan(pi/(2*C)))/(B*xm - atan(B*xm)));
x= s(p) + Sh;
y(p)= D*sin(C*atan((B*s(p))-E*(B*s(p)-atan(B*s(p)))));
Y(p)= y(p) + Sv;
end
nexttile
plot (s,-Y)
title ('Magic Formula Long. force')
xlim([-1 1])
ylim([-5000 5000])
for p=1:length (alp)
Ccl = (2/pi)*asin(ys/D);
E= ((B*xm - tan(pi/(2*C)))/(B*xm - atan(B*xm)));
ys= D*sin((pi/2)*C);
x= alp(p) + Sh;
y(p)= D*sin(C*atan((B*alp(p))-E*(B*alp(p)-atan(B*alp(p)))));
Y(p)= y(p) + Sv;
end
nexttile
plot (alp,Y)
title ('Magic Formula lat. force')
xlim([-1 1])
ylim([-5000 5000])
Hello,
My code was working plotting 4 graphs, a path of 2 plots looking similar to each other. But after downloading syms toolbox and try to run it it just gives me an error..
Unrecognized function or variable 'ys'.
Error in Homework_1 (line 55)
C = (2/pi)*(asin(ys/D));
I dont know if its because the syms toolbox but something happened. it could maybe be a file error or something in that manner.
2 Comments
Stephen23
on 18 Feb 2022
Do NOT "throw in" code like CLEAR ALL, which clears all cached functions from memory, so pointlessly slows down your code every time you run it as they get cached again. The CLEAR documentation states "Calling clear all decreases code performance, and is usually unnecessary". It is an example https://en.wikipedia.org/wiki/Anti-pattern
A simple CLEARVARS is most likely quite sufficient, if required.
Answers (1)
John D'Errico
on 18 Feb 2022
Edited: John D'Errico
on 18 Feb 2022
This has ABSOLUTELY NOTHING to do with the symbolic toolbox, or your download of that TB.
LOOK AT YOUR CODE. READ THE ERROR MESSAGE. What does MATLAB say/think is the problem?
Unrecognized function or variable 'ys'.
Error in Homework_1 (line 55)
C = (2/pi)*(asin(ys/D));
Ok. so it does not know what is ys. Where is ys defined?
for p=1:length (s)
C = (2/pi)*(asin(ys/D));
ys= D*sin((pi/2)*C);
E= ((B*xm - tan(pi/(2*C)))/(B*xm - atan(B*xm)));
x= s(p) + Sh;
y(p)= D*sin(C*atan((B*s(p))-E*(B*s(p)-atan(B*s(p)))));
Y(p)= y(p) + Sv;
end
So the line where you use the variable ys, comes BEFORE you defined the variable ys. Do you see a problem here? You cannot use something that does not currently exist, that does not have a value already assigned.
You use ys to create C. But then you use C to define ys. And no place in the code before that point did you ever define ys.
Did this work in the past? POSSIBLY. Had you defined a variable named ys previously, BEFORE you ran the code, then ys would have been defined when you try to use it to define C. Of course, we have no idea what ys contained in that past instance. So there is no guessing what was happening, because we have no idea how ys was previously defined. My guess is, neither do you.
Again, this has absolutely nothing to do with your download of the symbolic toolbox, EXCEPT that when you downloaded the syms TB, you restarted MATLAB. And that will have caused you to start with an empty workspace. So there, ys never existed in advance, so your code failed.
You may either need to predefine a value for ys that makes some sense as an initial value, or you may need to use a solver, to decide which values of C and ys will simultaneously satisfy those two equations. Since we have no idea what C and ys do, this is something only you can decide.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!