Why my data is not plotting the graph using plot(Q,h)

Hi,
I am not sure what is happening here? I am using the code below and all working fine but whenever it supposed to plot the graph it is not plotting the graph instead it is just printing all the values in command line section. So, I am using plot (Q,h) but whenever, i used scatter (Q,h) it put the points on the graph but for data representations I will need to plot the curves rather then scatter points.
I even used this code to join the scatter points but no result.
Code 2:
scatter(Q,h,'.','LineWidth',1);
line(Q,h,'Color','k','LineStyle','-','LineWidth',5)
hold on
Code1:
for Q = [0:1:100];
if h < h1
v_2 = ((Q/1000) / Area_Outlet1)
h = ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2^2 - v_1^2)) + h_f)*1000
else
v_2a = ((Q/1000) / Area_Outlet2)
h = h1 + ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2a^2 - v_1^2)) + h_f)*1000
end
plot (Q,h)
end
Any Help?

 採用された回答

KSSV
KSSV 2020 年 10 月 5 日
編集済み: KSSV 2020 年 10 月 5 日

1 投票

q = [0:1:100];
h = zeros(size(q)) ;
for Q = 1:length(q)
if val < h1 % < --- think of this line
v_2 = ((Q/1000) / Area_Outlet1)
h(Q) = ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2^2 - v_1^2)) + h_f)*1000
else
v_2a = ((Q/1000) / Area_Outlet2)
h(Q) = h1 + ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2a^2 - v_1^2)) + h_f)*1000
end
end
plot (q,h)

8 件のコメント

muhammad choudhry
muhammad choudhry 2020 年 10 月 5 日
what does val stand for ?
in my case h = 0 and h1 = 500.
ehenever I ran you code as it is it told that val is un recongnized but then I change to 0 hence code run but then again no plot here
muhammad choudhry
muhammad choudhry 2020 年 10 月 5 日
I see you edited the answer but still it did not resolve the problem.... so after using the edited answer, I am getting the curve for the section in the loop before else and the condition after else is not being taking into account.
KSSV
KSSV 2020 年 10 月 5 日
How h = 0 and you are savinh result in h?
muhammad choudhry
muhammad choudhry 2020 年 10 月 5 日
Let me Explain: I am filling a tank with one inlet and same tank is getiing empty with 2 outlet with different height in the tank hence I am explaining in the code that from height 0 to 500 mm there is only one outlet above 500 mm there is 2 outlets now so use if condition for one outlet or else for 2 outlets that's why you can see there is 2 calculations in the loop. So, untill 500 graph will rise, but after 500 there is different scenario. all that is great. Problem is occuring because Q is explained in array and h is calculating as a number so it is not plotting a plot rather working as a scatter plot Please see below:
%Logic for two outlets
h = 0; %initial height
h1 = 500 % two outlets
for Q = [0:5:100];
if h < h1
v_2 = ((Q/1000) / Area_Outlet1);
h = ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2^2 - v_1^2)) + h_f)*1000
else
v_2a = ((Q/1000) / Area_Outlet2);
h = h1 + ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2a^2 - v_1^2)) + h_f)*1000
end
scatter(Q,h,'.','LineWidth',1);
hold on
end
muhammad choudhry
muhammad choudhry 2020 年 10 月 5 日
How can i convert calculated h values into array ? this will solve the problem. so it will be like this
Q = [0,5,100]
h = [a,b,c,d,..........]
plot (Q,h)
at a moment I am getting my h value as double and it is not saving as an array.
KSSV
KSSV 2020 年 10 月 5 日
%Logic for two outlets
h = 0; %initial height
h1 = 500 % two outlets
q = 0:5:10
h = zeros(size(q)) ;
h(1) = 0 ; % initial height
for i = 2:length(q)
Q = q(i) ;
if h(i-1) < h1
v_2 = ((Q/1000) / Area_Outlet1);
h(i) = ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2^2 - v_1^2)) + h_f)*1000
else
v_2a = ((Q/1000) / Area_Outlet2);
h(i) = h1 + ((1 / (rho * g)) * (Patm2 - Patm1 + 0.5 * rho * (v_2a^2 - v_1^2)) + h_f)*1000
end
end
plot(q,h)
muhammad choudhry
muhammad choudhry 2020 年 10 月 5 日
Thanks alot that's work. But can you explain this code to me how is it different from the code I had initially, and what sort of changes you made? so, i do not run into problem in future.
I will be very thankful to you.
KSSV
KSSV 2020 年 10 月 5 日
You want to save the output into a array h. So you have to update the value of h in the loop. The updated values of h should be comapred in the if else. You can see the code and relaize yourself where changes are made.
Thanks is accepting/ voting the answer. :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2020 年 10 月 5 日

コメント済み:

2020 年 10 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by