How do I get the minimum/maximum of one of the graphs I have plotted?
2 views (last 30 days)
Show older comments
Dakota Fike
on 9 Sep 2018
Commented: Walter Roberson
on 9 Sep 2018
a=[.2:.01:1];
E_A=(-1.436)./a;
E_R=(5.86*10.^(-6))./(a.^9);
E_N=E_A+E_R; % <<<<<<----- I am trying to find the min of this equation but i cant figure out how to
plot(a,E_A);
hold on
plot(a,E_R);
plot(a,E_N);
title('Energy vs. Distance between atoms')
ylabel('Energy (eV)')
xlabel('Distance between atoms (nm)')
legend('E_A','E_R','E_N')
0 Comments
Accepted Answer
Walter Roberson
on 9 Sep 2018
[min_E_N, min_pos] = min(E_N);
[max_E_N, max_pos] = max(E_N);
a_at_min = a(min_pos);
a_at_max = a(max_pos);
plot(a_at_min, min_E_N, 'rv', a_at_max, max_E_N, 'g^')
3 Comments
Walter Roberson
on 9 Sep 2018
min_pos is the index within E_N at which the minimum occurs. When you give only E_N, MATLAB cannot know which a generated that particular value, but it can know the relative position the minimum was found in, and then you can use that relative position to look up the associated a value.
For creating the data tips, I adapt code from https://undocumentedmatlab.com/blog/controlling-plot-data-tips
a=[.2:.01:1];
E_A=(-1.436)./a;
E_R=(5.86*10.^(-6))./(a.^9);
E_N=E_A+E_R; % <<<<<<----- I am trying to find the min of this equation but i cant figure out how to
plot(a,E_A);
hold on
E_r_line = plot(a,E_R);
E_N_line = plot(a,E_N);
title('Energy vs. Distance between atoms')
ylabel('Energy (eV)')
xlabel('Distance between atoms (nm)')
legend('E_A','E_R','E_N')
[min_E_N, min_pos] = min(E_N);
[max_E_N, max_pos] = max(E_N);
a_at_min = a(min_pos);
a_at_max = a(max_pos);
cursorMode = datacursormode(gcf);
datatip(1) = cursorMode.createDatatip(E_N_line);
datatip(2) = cursorMode.createDatatip(E_N_line);
datatip(1).Position = [a_at_min, min_E_N, 0];
datatip(2).Position = [a_at_max, max_E_N, 0];
More Answers (0)
See Also
Categories
Find more on Modify Image Colors in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!