How can I run the calculation of one equation through a range of variables in one loop

조회 수: 1(최근 30일)
Stuart Usher
Stuart Usher 2021년 10월 19일
댓글: Stuart Usher 2021년 10월 20일
Hi
I am inexperienced with MATLAB and new to this forum.
I and am trying to calculate weight transfer at varying inclines at varying amount of acceleration and get a range of values out which i can then calculate the amount of tyre deflection and plot (Wf, Ax), (Wf, S_T), and (Dt, Wf).
I am ok with plotting.
I hope you can help
Thanks in advance
Stu
i = 1;
for S_T = 0:3:60
TS_T =(deg2rad(S_T));%incline in Radians
TGDs = sin(TS_T); %Sine of incline
TC_T =(deg2rad(S_T));%incline in Radians
TGDc = cos(TC_T); %Cosine of incline
for Ax = 0:0.25:5
Wf(i)=(((W*cWB*TGDc)-(W*hCoG*TGDs)-((W/g)*Ax*hCoG))/WB)/9.81;
end
if Wf>=0
Wfo = Wf;
Dt = (0.5*Wfo)/(0.00028*P*d+3.45);
end
end
i=i+1;

답변(2개)

Walter Roberson
Walter Roberson 2021년 10월 19일
S_T = 0:3:60; %row vector
TGDs = sind(TS_T); %Sine of incline
TGDc = cosd(TC_T); %Cosine of incline
Ax = (0:0.25:5).'; %column vector
Wf = (((W .* cWB .* TGDc) - (W .* hCoG .* TGDs) - ((W./g) .* Ax .* hCoG))./WB)./9.81;
Wfo = nan(size(Wf));
Dt = nan(size(Wf));
mask = Wf > 0;
Wfo(mask) = Wf(mask);
Dt(mask) = (0.5*Wfo(mask)) ./ (0.00028 .* P .* d + 3.45);
This will give you results that are length(ST) columns and length(Ax) rows.
Your existing code only copies to Wfo and Dt for cases where Wf are positive -- leaving it undefined what should happen if Wf <= 0. I duplicate that behaviour as best practical by writing NaN into Wfo and Dt and only putting in other values for the locations where Wf > 0
  댓글 수: 1
Stuart Usher
Stuart Usher 2021년 10월 20일
Hi Walter
Thanks for your reply.
This still does not produce what i need, which is the output from the equation calculated using the S-T 0:1:60 (61 calculations) while at Ax 0:0.25:5 (21 calculations) totaling 1281 calcutation of Wf
Thanks for your help
Stuart

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2021년 10월 19일
You can proceed something like below. No need to use loops.
i = 1;
S_T = 0:3:60 ;
TS_T =(deg2rad(S_T));%incline in Radians
TGDs = sin(TS_T); %Sine of incline
TC_T =(deg2rad(S_T));%incline in Radians
TGDc = cos(TC_T); %Cosine of incline
Ax = 0:0.25:5;
Wf=(((W*cWB*TGDc)-(W*hCoG*TGDs)-((W/g)*Ax*hCoG))/WB)/9.81;
Wfo = Wf(Wf>=0) ;
Dt = (0.5*Wfo)/(0.00028*P*d+3.45);

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by