Help With For Loops in Plotting Problem

3 ビュー (過去 30 日間)
Not_sharpest_tool.mat
Not_sharpest_tool.mat 2019 年 7 月 25 日
編集済み: per isakson 2019 年 7 月 25 日
I have written this formula and it works well...
The thing is I need to use For loops for the values of "Zeta" to change. I need to be able to plot four graphs with values 0.2, 0.4, 0.6, 0.8 for Zeta.
Should I use the For loop on the C_t formula? or would it be better for only the value of Zeta to change? Either way I can't seem to make it work. Any suggestions on how to proceed?
Zeta=0.2;
Nat_fr=2.5; %Natural Frequency
Beta= sqrt(1-Zeta.^2);
Tan_theta=Beta/Zeta;
theta=atan(Tan_theta); %Angle in radians
t= linspace(0,15,100);
%% Equation
c_t= 1-(1/(sqrt(1-Zeta.^2))).*(exp(-Zeta.*Nat_fr.*t)).*sin((sqrt(1-Zeta)).*Nat_fr.*t+atan((sqrt(1-Zeta))/Zeta));
%% Plot
plot(t,c_t)

採用された回答

Raj
Raj 2019 年 7 月 25 日
編集済み: Raj 2019 年 7 月 25 日
temp=1;
t= linspace(0,15,100);
Nat_fr=2.5; %Natural Frequency
for Zeta=0.2:0.2:0.8
Beta (temp,1)= sqrt(1-Zeta.^2);
Tan_theta(temp,1)=Beta(temp,1)/Zeta;
theta(temp,1)=atan(Tan_theta(temp,1)); %Angle in radians
%% Equation
c_t= 1-(1/(sqrt(1-Zeta.^2))).*(exp(-Zeta.*Nat_fr.*t)).*sin((sqrt(1-Zeta)).*Nat_fr.*t+atan((sqrt(1-Zeta))/Zeta));
%% Plot
plot(t,c_t)
hold on
temp=temp+1;
end
legend ('Zeta=0.2','Zeta=0.4','Zeta=0.6','Zeta=0.8');
grid on;

その他の回答 (1 件)

per isakson
per isakson 2019 年 7 月 25 日
編集済み: per isakson 2019 年 7 月 25 日
Either should work.
I prefer to keep plot() outside the loop because of speed. That requires to make c_t a matrix, which in turn requires an interger loop-counter to reference c_t. Thus, something like this
function cssm1()
Nat_fr=2.5; %Natural Frequency
Zeta = [0.2,0.4,0.6,0.8];
t = linspace(0,15,100);
c_t = nan( length(Zeta), length(t) ); % preallocate
for jj = 1 : length( Zeta )
% Beta= sqrt(1-Zeta(jj).^2);
% Tan_theta=Beta/Zeta(jj);
% theta = atan(Tan_theta); %Angle in radians
%% Equation
c_t(jj,:) = 1-(1/(sqrt(1-Zeta(jj).^2))).*(exp(-Zeta(jj).*Nat_fr.*t)).*sin((sqrt(1-Zeta(jj))).*Nat_fr.*t+atan((sqrt(1-Zeta(jj)))/Zeta(jj)));
end
%% Plot
plot( t, c_t )
end
sqrt(1-Zeta(jj)) is calculated three times in the equation, which is a bit of waste of cpu-cycles. Ok, that's why you calculate Beta.
I put the code in a function to avoid cluttering of my base workspace.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by