Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: matlab novice
Replies: 12   Last Post: Aug 1, 2013 7:42 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Torsten Hennig

Posts: 2,419
Registered: 12/6/04
Re: matlab novice
Posted: Aug 1, 2013 4:10 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

> im trying to get these if statements to work but its
> not doing what i want, i want it to use separate
> calcs when M_excess > 0 and when M_excess < 0. There
> are negative values in vector M_excess.
>
> please help!
>
> titration script is
>
> Kw = 1e-14;
> Ka = 1.755388e-5;
> pKa = 4.756;
> %
> Ca = 0.5;
> Cb = 0.1;
> Va = 100;
> Vb = 0:0.1:1500;
> %
> % This simulates initial molarity of CH3CO2H.
> Ma = (Ca * Va) / 1000;
> % This simulates initial molarity of NaOH.
> Mb = (Cb .* Vb) ./ 1000;
> % This simulates the displacement of H ions.
> M_excess = Ma - Mb;
> if M_excess > 0
> % This simulates final molarity of CH3CO2H.
> Ma_final = (M_excess .* 1000) ./ (Va + Vb);
> % This simulates final molarity of CH3CO2-.
> Mb_final = (Mb .* 1000) ./ (Va + Vb);
> % This simulates HPlus ions.
> Hplus = Ka .* (Ma_final ./ Mb_final);
> else OH = (M_excess .* 1000 * -1) ./ (Va + Vb);
> % This simulates HPlus ions.
> Hplus = Kw ./ OH;
> end
> % This simulates the pH content.
> pH = -log10(Hplus);


The first part of your if-statement is only executed
if _all_ elements of the array M_excess are positive.
If this is not the case, the second part of the if-statement is executed.
I think this is not what you want - you want the if-statement to be executed elementwise.
For this, use a for-loop:

For i=1:length(Mb)
M_excess(i)=Ma-Mb(i);
if M_excess(i)>0
...
else
...
end
end

Best wishes
Torsten.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.