displaying indexed values correctly

2 次查看(过去 30 天)
hi, trying to display some values but am not sure how to go about it. I want to do is; find Vb at half of Vb_ev and then find the corresponding pH value at that point. see last 2 lines. any help will be appreciated.
format short
Kw = 1e-14;
Ka = 1.755388e-5;
Ca = 0.5;
Cb = 0.1;
Va = 100;
Vb = 0.05:0.05:2000;
Ma = (Ca * Va) / 1000;
Mb = (Cb .* Vb) ./ 1000;
for i = 1:length(Mb)
M_excess = Ma - Mb(i);
if abs(M_excess)<eps
Hplus = Ka * ((Ma_final * 0.999999) ./ Mb_final);
Vb_ev = Vb(i);
pH_ev = pH(end);
elseif M_excess > 0
Ma_final = (M_excess * 1000) ./ (Va + Vb(i));
Mb_final = (Mb(i) * 1000) ./ (Va + Vb(i));
Hplus = Ka * (Ma_final ./ Mb_final);
elseif M_excess < 0
OH = (M_excess * 1000 * (-1)) ./ (Va + Vb(i));
Hplus = Kw ./ OH;
end
pH(i) = -log10(Hplus);
end
%
%
Vb_Mid_Pt1 = uint16(Vb_ev)*0.5;
pH_Mid_Pt1 = pH(Vb_Mid_Pt1)
i tried the above but the pH_Mid_Pt1 gives me the value in the pH array that is equal to the actual numerical value of that from (Vb_ev)*0.5.
  9 个评论
Walter Roberson
Walter Roberson 2013-8-7
If half Vb_ev is 250 then is Ph(250) what you want returned? If so then why do you consider your current code to be incorrect?
Are you looking for the point in Ph that is closest to Ph(Vb_ev)/2 ?
harley
harley 2013-8-7
its gives me the pH value in column 250 of the pH array, (using above code), i want it to give me the value of pH when Vb = 250 (half of Vb_ev). thanks again.

请先登录,再进行评论。

采纳的回答

Richard Brown
Richard Brown 2013-8-7
Similar to what you asked before, I think what you want is to find the Vb element that is closest to half of Vb_ev, and then pluck out the corresponding pH element. The answer is very similar to what I gave you last time:
[~, idx] = min(abs(Vb - Vb_ev/2));
Vb_Mid_Pt1 = Vb(idx);
pH_Mid_Pt1 = pH(idx);
I think what's throwing you off is the distinction between values and indices. You are wanting to find certain indices in your array that have values near other specific ones. Hence the use of min with abs, which provides the index of the closest point.
  3 个评论
Walter Roberson
Walter Roberson 2013-8-7
Note: different versions of MATLAB may return different results for the above. If there are multiple elements with the same difference (as would be the case if Vb_ev/2 falls between two Vb values), then some versions will return the index of the first of them, and some will return the index of the last of them.
Richard Brown
Richard Brown 2013-8-7
Indeed. And for this problem it is likely that you'd end up halfway between two sample values. Using interpolants would be the next step in sophistication

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by