Variable "H" might be set by a nonscalar operator

Hello,
My goal for my code is to simulate a tire model and H equation gets a error code saying "Variable H might be set by a nonscalar operator". I think it has to deal with my variable s being defined as a vector but I dont know how to change it into a scalar. Help please :).

1 Comment

Please post code as text, not as screenshot. Then it is easier to suggest modifications using copy&paste.

Sign in to comment.

Answers (1)

If H is an array, what is the meaning of:
if H < 0.5
? The IF command requires a scalar condition. Therefore Matlab inserts an all() automatically. To be exact:
if all(H(:) < 0.5) && ~isempty(H)
It is not meaningful "to change H to a scalar" also.
You are running a loop over the elements of s. Maybe you do not want to access the vector s, but the elements s(i).

8 Comments

When you say the loop should access s(i) should i define that after my script line 36? Will this mean the equation H will calculate all the values of range of s. When ever i run the script, I am only getting one output for H but I want to get as many outputs as my inputs, s being the input of a matrix 1x201.
As said already: I could easily post the modified code, if you provide the code as text and not as screenshot. I hestitate to type your code again, if you have it as text available already.
u= 0.9 ; % friction coefficient
Fz = 4500 ; % Newtons
As = 0 ; % friction discount factor
Cx = 80000 ; % lateral stiffness
Cy = 47275 ; % tire longitudinal
alp = (-20:0.02:20) ; % 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
plot (s,Fx)
xlim([-1 1])
ylim([-5000 5000])
Hello Jan!
I was able to plot my (alp,Fy) correctly, but im trying to plot (s,Fx) but my graph should look like the same graph from (alp,Fy) but reflected from the x-axis. I check my equation multiple times but nothing needs to be changed for my Fx(i)'s. I cant seem to find any other mistakes in my script.
Thank you.
You can simplify:
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
to
H = sqrt(((Cx * s) .^ 2) + (Cy * tan(alp)) .^ 2) ./ (u * Fz * (1 + s));
and the loop to:
for i = 1:length(H)
if H(i) < 0.5
Fx(i) = Cx * s(i) / (1 + s(i));
Fy(i) = Cy * tan(alp(i)) / (1 + s(i));
else
Fx(i) = Cx * (s(i) / (1 + s(i))) * ...
(1 / H(i) - 1 / (4 * H(i) ^ 2));
Fy(i) = Cy * ((tan(alp(i))) / (1 + s(i))) * ...
(1 / H(i) - 1 / (4 * H(i) ^ 2));
end
end
Now it looks more clear. Are you sure with the parentheses for Fx and Fy?
i changed the parentheses to brackets. ima try this out!
Unforunately this didn't work for me, my (alp,Fy) graph changed competely and (s,Fx) isnt what im looking for. Thank you for the help i think ima just give up on this for right now.
Jan
Jan on 14 Feb 2022
Edited: Jan on 14 Feb 2022
Square brackets are the operator for concatenating arrays. Changing parentheses to brackets is not useful.
My suggested code produces the same output as the original code.
"(s,Fx) isnt what im looking for" - I do not know, what you are looking for. All I know is the code you have posted.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 13 Feb 2022

Edited:

Jan
on 14 Feb 2022

Community Treasure Hunt

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

Start Hunting!