MATLAB Answers

why my matlab didn't plot anything?

3 ビュー (過去 30 日間)
Muhammad Hilmi Al Farid Ni'man
Muhammad Hilmi Al Farid Ni'man 2021 年 9 月 10 日
編集済み: DGM 2021 年 9 月 10 日
So i tried to plot a graph using this code, but it won't plot anything. I believe my code is plotting a point but not a line and i don't know why
clear all
m = 85
m = 85
g = 9.81
g = 9.8100
ti = 0
ti = 0
tf1 = 9
tf1 = 9
v(1)= -20
v = -20
h = 1
h = 1
c1 = 11
c1 = 11
dv1 = @(g,c1,v,m) g-c1*v/m
dv1 = function_handle with value:
@(g,c1,v,m)g-c1*v/m
while(ti<tf1)
v = v + dv1(g,c1,v,m)*h
ti = ti+h
plot(ti,v)
drawnow
end
v = -7.6018
ti = 1
v = 3.1920
ti = 2
v = 12.5889
ti = 3
v = 20.7698
ti = 4
v = 27.8919
ti = 5
v = 34.0924
ti = 6
v = 39.4904
ti = 7
v = 44.1899
ti = 8
v = 48.2812
ti = 9

回答 (3 件)

Matt J
Matt J 2021 年 9 月 10 日
編集済み: Matt J 2021 年 9 月 10 日
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;
k=1;
while(ti<tf1)
k=k+1;
v(k) = v(k-1) + dv1(g,c1,v(k-1),m)*h;
ti=ti+h;
plot(0:h:ti,v); drawnow
end

Star Strider
Star Strider 2021 年 9 月 10 日
The plot call plots points here, so it is necessary to plot markers. The plot funciton draws lines between points, so its arguments otherwise must be at least 2-element vectors.
Try this —
m = 85
m = 85
g = 9.81
g = 9.8100
ti = 0
ti = 0
tf1 = 9
tf1 = 9
v(1)= -20
v = -20
h = 1
h = 1
c1 = 11
c1 = 11
dv1 = @(g,c1,v,m) g-c1*v/m
dv1 = function_handle with value:
@(g,c1,v,m)g-c1*v/m
figure
hold on
while(ti<tf1)
v = v + dv1(g,c1,v,m)*h
ti = ti+h
plot(ti,v, 'p')
drawnow
end
v = -7.6018
ti = 1
v = 3.1920
ti = 2
v = 12.5889
ti = 3
v = 20.7698
ti = 4
v = 27.8919
ti = 5
v = 34.0924
ti = 6
v = 39.4904
ti = 7
v = 44.1899
ti = 8
v = 48.2812
ti = 9
hold off
Experiment to get different results.
.

DGM
DGM 2021 年 9 月 10 日
編集済み: DGM 2021 年 9 月 10 日
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;
for k = 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.

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by