How to find maximum value and corresponding x-value of multiple graphs?

조회 수: 1 (최근 30일)
here is my code:
z1=zeros(length(fv),1);
z2=zeros(length(fv),1);
z3=zeros(length(fv),1);
O=zeros(length(fv),1);
for n=1:length(fv)
f=(fv(n));
om=(2*pi*f);
om2=(om.*om);
fhat=((k1+i*om*c1)*Stak)*[0;1;(exp(i*2*pi*L/y));0];
A=(K+i*om*C-om2*M);
zhat=A\fhat;
z1(n)=zhat(1);
z2(n)=zhat(2);
z3(n)=zhat(3);
O(n)=zhat(4);
end
plot(fv,abs(z1),fv,abs(z2),fv,abs(z3),fv,abs(O));
xlabel('Speed, [m/s]');
ylabel('Displacement, [m]');
grid;
hold on;
This gives me 4 graphs and i want Identify maxima of the amplitudes (y-values, "z1,z2,z3,O"), and the corresponding vehicle speed (x-values, "fv")

채택된 답변

Rik
Rik 2021년 4월 16일
This is trivial if you use an array instead of numbered variables:
z{1}=zeros(numel(fv),1);
z{2}=zeros(numel(fv),1);
z{3}=zeros(numel(fv),1);
% ^^^^ use numel or size instead of length
O=zeros(nmumel(fv),1);
for n=1:numel(fv)
f=fv(n);
om=2*pi*f;
om2=om.*om;
fhat=((k1+i*om*c1)*Stak)*[0;1;(exp(1i*2*pi*L/y));0];
% ^^ use 1i or 1j to avoid collision
% with i as a common variable name
A=K+i*om*C-om2*M;
zhat=A\fhat;
for m=1:3
z{m}(n)=zhat(m);
end
O(n)=zhat(end);
% ^^^ this replacement assumes there are only 4 elements
end
plot(fv,abs(z{1}),fv,abs(z{2}),fv,abs(z{3}),fv,abs(O));
xlabel('Speed, [m/s]');
ylabel('Displacement, [m]');
grid;
%hold on;
% ^^ you aren't adding any plots, so why use hold on?
And now you can use a simple loop to determine whichever maximum you mean exactly.
Instead of a cell array, you could also make z a 2D array:
z=zeros(numel(fv),3);
...
z(n,:)=zhat(1:3);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by