为什么我的matlab anythi情节吗ng?
1视图(最近30天)
Show older comments
因此,我尝试使用此代码绘制图形,但不会绘制任何内容。我相信我的代码是绘制一个点,但没有线,我不知道为什么
清除all
m = 85
g = 9.81
ti = 0
tf1 = 9
v(1)= -20
h = 1
c1 = 11
dv1 = @(g,c1,v,m) g-c1*v/m
while(ti
v = v + dv1(g,c1,v,m)*h
ti = ti+h
plot(ti,v)
drawnow
end
0 Comments
Accepted Answer
More Answers (1)
DGM
on 10 Sep 2021
Edited:DGM
on 10 Sep 2021
Start with something like this:
m = 85;
g = 9.81;
ti0 = 0;
tf1 = 9;
v0 = -20;
h = 1;
c1 = 11;
dv1 = @(g,c1,v,m)g-c1*v/m;
ti = ti0:h:tf1;
v = zeros(1,numel(ti));
v(1) = v0;
fork = 2:numel(ti)
v(k) = v(k-1) + dv1(g,c1,v(k-1),m)*h;
end
plot(ti,v)
Plotting one point at a time is generally unnecessary and cumbersome. When you plot single points that way with the default linestyle, they are not connected by lines. You could make the dots themselves visible by specifying a marker type other than the default, but storing the results and putting the plot statement outside the loop avoids the hassle and leaves you with vectors that can be used for other things if needed.
It's also generally good to avoid unnecessary while loops, using for-loops when the number of iterations is known beforehand. This avoids any chance of mistakes or unpredicted scenarios that could cause the loop to not terminate. Such an oversight isn't really a big risk with something this small, but it's something to keep in mind as your code gets more complex.
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!