My plot is not showing a line? Why?

145 ビュー (過去 30 日間)
Frederick Anokye
Frederick Anokye 2020 年 1 月 26 日
コメント済み: Walter Roberson 2020 年 1 月 26 日
%Plot of net energy,attractive energy and repulsive energy of a two
%isolated ions seperated by a distance
% r is interatomic distance between the ions
clc; clear;
A=1.436; B=7.32*10^-6; n=8;
r = 0:0.1*10^-9:1*10^-9;
Net_Energy = zeros(length(r),1);
Attractive_Energy = zeros(length(r),1);
Repulsive_Energy = zeros(length(r),1);
for i=1:length(r)
Net_Energy(i) = -A/r(i) + B/r(i)^n;
Attractive_Energy(i) = -A/r(i);
Repulsive_Energy(i) = B/(r(i)^n);
end
for i=1:length(r)
plot(r(i),Net_Energy(i),'g')
hold on
plot(r(i),Attractive_Energy(i),'g')
hold on
plot(r(i)*10^9,Repulsive_Energy(i),'g')
hold on
end

回答 (3 件)

Eddie
Eddie 2020 年 1 月 26 日
Replace this
for i=1:length(r)
plot(r(i)*10^9,Net_Energy(i),'g')
hold on
end
with
f=figure; ax=axes(f);
plot(ax,r*10^9,Net_Energy,'g');
If you want to plot a line, the data need to be a single input to plot function instead of using a for loop, which would only plot your data points as individual points.
  2 件のコメント
Frederick Anokye
Frederick Anokye 2020 年 1 月 26 日
I have edit my code check again and comment
Eddie
Eddie 2020 年 1 月 26 日
Remove the for loop, use this instead
plot(r,Net_Energy,'g')
hold on %only need to do it once
plot(r,Attractive_Energy,'g')
plot(r*10^9,Repulsive_Energy,'g')
However, the last plot (Repulsive_Energy) won't appear with the other plots in the same region (since ther previous two plots are in region r, while the last one is in region r*10^9. I suspect this is what you want -
plot(r*10^9,Net_Energy,'g')
hold on %only need to do it once
plot(r*10^9,Attractive_Energy,'g')
plot(r*10^9,Repulsive_Energy,'g')

サインインしてコメントする。


Walter Roberson
Walter Roberson 2020 年 1 月 26 日
Remove the plotting loop and use
plot(r*10^9,Net_Energy,'g')
  1 件のコメント
Frederick Anokye
Frederick Anokye 2020 年 1 月 26 日
Still not working

サインインしてコメントする。


Frederick Anokye
Frederick Anokye 2020 年 1 月 26 日
Thanks!
How can I make the lines curvy
  2 件のコメント
Frederick Anokye
Frederick Anokye 2020 年 1 月 26 日
How can I label the lines
Walter Roberson
Walter Roberson 2020 年 1 月 26 日
use semilogy() instead of plot() to make the lines curvy.
To label the lines, use legend()

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by