why is my plot not showing anything
9 次查看(过去 30 天)
显示 更早的评论
clc
k = 8.9870e+009;
q1 = .000000024820;
q2 = .000005200901;
for r = [.10,.15,.20,.25,.30,.40];
f = (k*q1*q2)/r.^2
end
plot(r,f)
hold on
axis([0 .5 0 .2])
i want to plot f vs r
4 个评论
Christopher
2025-3-1
I am having a similar problem.
I have two defined vectors, and when I go to plot them, it shows me a window with a blank plot.
code (simplified because there is a lot of other code)
query=input("question")
mat1=(1:500);
mat2=linspace(0,query,500);
plot(mat2,mat1,"-",color='blue')
Walter Roberson
2025-3-1
That code looks okay, provided that a finite value is entered for "query" and the finite value exceeds 500*eps(0)
Is it possible that you have a third-party plot.m or that you have assigned to a variable named plot ?
采纳的回答
Oleg Komarov
2012-5-15
You are overwriting f at every iteration and in the end you just have one point.
plot(r,f)
will plot thus one point. If you look carefully you'll find it.
Try this, the vectorized code:
k = 8.9870e+009;
q1 = .000000024820;
q2 = .000005200901;
r = [.10,.15,.20,.25,.30,.40];
f = (k*q1*q2)./r.^2
plot(r,f)
hold on
axis([0 .5 0 .2])
0 个评论
更多回答(2 个)
Thomas
2012-5-15
try
k = 8.9870e+009;
q1 = .000000024820;
q2 = .000005200901;
r = [.10,.15,.20,.25,.30,.40];
for count=1:numel(r)
f(count) = (k*q1*q2)/r(count).^2 % save f as array
end
plot(r,f)
hold on
axis([0 .5 0 .2])
You were not saving f as array and hence were only plotting one f and one r pair..
You can avoid the loop totally as well using:
f = (k*q1*q2)/r.^2;
plot(r,f)
rajdeep singh
2020-2-9
What is r is a function of random numbers? Like data points
1 个评论
Walter Roberson
2020-2-9
Oleg's and Thomas's solutions do not care how you create r and do not care whether r is sorted.
The one thing to watch out for if r is not sorted is that because by default plot() connects points that are adjacent in data order, you would get a lot of lines back and forth across the screen. sorting on r prevents that problem.
for idx=1:10
r(idx) = rand() ;
f(idx) = (k*q1*q2)/r(idx).^2;
end
[sr, sidx] = sort(r) ;
sf = f(sidx);
plot(sr, sf)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!