no plot appearing for loop
3 ビュー (過去 30 日間)
古いコメントを表示
when i run this code i have the plot screen appear but no data on it. can anbody help me please?
f = 1:0.1:1;
h = 0:0.1:4;
for i=1:length(f)
for j=1:length(h)
t=0;
L = 1;
m = 7+L;
u = 0.042;
Um = h(i) * (f(i));
r = Um * -cos(f(i) * t);
dy = Um*r(i)*sin(f(i)*t);
df = 1+L;
p = ((df * u)/m) * Um;
B = (df * (u^2) * f(i))/(2 * pi * m);
a = p/B;
g = 1 + ((pi * B)^(-0.5));
c = df*u^2;
Cd = (pi*B)^(-0.5);
n = r*abs(r)*u*Cd;
e=((c + n));
plot(f,e(i),'Linewidth',1.5);
hold on
xlabel('x');
ylabel('y');
end
end
1 件のコメント
Stephen23
2020 年 8 月 13 日
編集済み: Stephen23
2020 年 8 月 13 日
1- Your code only runs without error because this line happens to define a scalar value:
f = 1:0.1:1;
As soon as f has two or more elements (as that colon operator and the loop itself implies, otherwise they would be both completely superfluous) then your code will try to access the second element of many scalar variables and you will get a stream of errors (which is what happened when I tried it just now).
2- Probably most/all of the matrix operations should be replaced with array operations: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
3- I suspect that most/all of this code can be replaced with some simpler vectorized code: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
4- Probably you should replace (...)^(-0.5) with 1./sqrt(...)
5- Plotting lots of separate points like that is not a good use of MATLAB: at best it can give you a set of disconnected points. But because the default linestyle does not have a point marker, by default you will not get anything displayed in the axes. You could fix this by changing the linestyle to include a marker, but you still won't get a line (which you seem to expect, due to the use of the 'Linewidth' option).
The MATLAB approach is to store the data in an array and then plot it all at once after the loop.
6- There might be other bugs...
回答 (2 件)
Alan Stevens
2020 年 8 月 13 日
You have t = 0 every time you enter the inner loop, and you don't update it. This gives rise to zeros in subsequent lines.
0 件のコメント
hosein Javan
2020 年 8 月 13 日
"f" was scalar rather than vector. some constants needed to be defined outside of the loop. and the iteration-depandant variables are fixed. I just don't know what is the inner loop for. you need to clarify what you want.
f = 0:0.1:1;
h = 0:0.1:4;
t=0;
L = 1;
m = 7+L;
u = 0.042;
df = 1+L;
c = df*u^2;
for i=1:length(f)
for j=1:length(h)
Um(i) = h(i) * (f(i));
r(i) = Um(i) * -cos(f(i) * t);
dy(i) = Um(i)*r(i)*sin(f(i)*t);
p(i) = ((df * u)/m) * Um(i);
B(i) = (df * (u^2) * f(i))/(2 * pi * m);
a(i) = p(i)/B(i);
g(i) = 1 + ((pi * B(i))^(-0.5));
Cd(i) = (pi*B(i))^(-0.5);
n(i) = r(i)*abs(r(i))*u*Cd(i);
e(i) = ((c + n(i)));
end
end
plot(f,e,'Linewidth',1.5);
xlabel('x');
ylabel('y');
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!