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

3 views (last 30 days)
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")

Accepted Answer

Rik
Rik on 16 Apr 2021
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);

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!