Clear Filters
Clear Filters

Beginning and end of the curve isn't linked with points

2 views (last 30 days)
Here is my MWE:
clc;
close all;
%There are input constants
cP=0.05;
cN=0.05;
cMg=0.005;
logb0=2.88;
logb1=1.17;
logb2=4.85;
logb3=2.58;
logKs1=-12.6;
pKa1=-2.143;
pKa2=-7.205;
pKa3=-12.34;
pKb1NH3=-4.753355133;
%there is a range of x-axis (0-14)
pH=0:0.01:14;
%There are calculations of cH2PO4 cHPO4 cPO4, they have to be calculated for black curve equation (they are variables of final curve equation and depend on pH value)
Ka1=power(10,pKa1);
Ka2=power(10,pKa2);
Ka3=power(10,pKa3);
cH2PO4=cP.*Ka1.*(power(10,-pH)).^(2)./((power(10,-pH).^(3))+Ka1.*(power(10,-pH).^(2))+Ka1.*Ka2.*(power(10,-pH).^(1))+Ka1.*Ka2.*Ka3);
cHPO4=cP.*Ka1.*Ka2.*power(10,-pH)./((power(10,-pH).^(3))+Ka1.*(power(10,-pH).^(2))+Ka1.*Ka2.*(power(10,-pH).^(1))+Ka1.*Ka2.*Ka3);
cPO4=cP.*Ka1.*Ka2.*Ka3./((power(10,-pH).^(3))+Ka1.*(power(10,-pH).^(2))+Ka1.*Ka2.*(power(10,-pH).^(1))+Ka1.*Ka2.*Ka3);
%There is calculations of cNH4, it has to be calculated for black curve equation (it is variable of final curve equation and depends on pH value also)
pOH=14-pH;
Kb1=power(10,pKb1NH3);
cNH4=cN.*Kb1./(power(10,-pOH)+Kb1);
%there are some partial calculations for the black curve equation
b0=power(10,logb0);
b1=power(10,logb1);
b2=power(10,logb2);
b3=power(10,logb3);
Ks1=power(10,logKs1);
cOH=power(10,pH-14);
%this is the black curve equation
logcNP=Ks1./(cPO4.*cNH4).*(1+b1.*cH2PO4+b0.*cHPO4+b2.*cPO4+b3*cOH);
%there are calculated a crosssections of the black curve with magenta dashed line (cMg value of 0.005 at the beginning of the code)
Index_NP = find(diff(sign(logcNP - cMg)));
for i = 1:numel(Index_NP);
IndexRange_NP = max(1,Index_NP(i)-1) : min(numel(pH),Index_NP(i)+1);
Crossection_NP(i) = interp1(logcNP(IndexRange_NP),pH(IndexRange_NP), cMg);
end
%Here are boundary consitions of magenta curve (not dashed)
pH_NP=(Crossection_NP(1)<=pH&pH<=Crossection_NP(2));
%This is plot of the final curve (black), crossections (cyan points), magenta dashed line and magenta curve
semilogy(pH,logcNP,'-', 'color', 'k', 'MarkerFaceColor','w', 'MarkerEdgeColor', 'm');
box off;
hold on;
semilogy(pH(pH_NP),logcNP(pH_NP),'-', 'color', 'm', 'MarkerFaceColor','w', 'MarkerEdgeColor', 'm');
box off;
hold on;
plot([0 Crossection_NP(1)],[cMg cMg],'--', 'color', 'm', 'LineWidth',0.5);
box off;
hold on;
plot(Crossection_NP(1), cMg, 'oc', 'MarkerFaceColor', 'w' ,'MarkerSize', 5);
box off;
hold on;
plot(Crossection_NP(2), cMg, 'oc', 'MarkerFaceColor', 'w' ,'MarkerSize', 5);
box off;
hold on;
axis([0 14 0.000000000000000001 1000000000000000000]);
xlabel('pH (-)');
ylabel('c {(mol/dm^{3})}');
axis square;
grid on;
axis on;
box off;
hold off;
the MWE leads to full scale plot:
if you zoom for more detailed view of magenta curve beginning and end It can be seen:
As it can be seen from zoomed pictures, the magenta curve is not joined with cyan points. How can It be done - magenta curve joined with cyan points? Seems like it is a problem of plotting accuracy.
Thank you for the answer.

Accepted Answer

Askic V
Askic V on 22 May 2023
At the bottom of your script I have added the following code:
% Define number of digits
numDigits = 10;
% Enable data cursor mode
dcm = datacursormode(gcf);
set(dcm, 'DisplayStyle', 'datatip', 'SnapToDataVertex', 'on',...
'UpdateFcn', @(src, event) ...
{['X: ' num2str(event.Position(1), numDigits)],...
['Y: ' num2str(event.Position(2), numDigits)]});
And this is how it looks like:
This is done with the help of chatGPT.
  3 Comments
Askic V
Askic V on 23 May 2023
Edited: Askic V on 23 May 2023
I see, in that case, try to reduce the step (plot is drawing a diagram from discrete set of points, by connecting with lines.
Try this:
pH=0:0.001:14; % reduce step for example 10 times

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!