the code doesn't update the values

anyone can help with this matlab code it doesn't update the values of p and t
n=1
while n<20
p=10^((30.59051-8.2*log10(t))+(0.0024804*t)-(3142.31/t))
t=314+((6.324-p)/(6.66*(10^-4)*101.32));
n=n+1;
end

 Accepted Answer

dpb
dpb on 1 May 2014
Edited: dpb on 2 May 2014
Of course it does, it just doesn't do what you think it should, apparently...
ADDENDUM-
If you're also expecting to build vectors of t and p, then you need to tell Matlab that, too...
Several problems here...
  • First, t is undefined at the beginning of the loop
What is it to be to begin with. If you start w/ t=0, then
log10(0)-->-Inf
and you divide by it later on, too. Solve those issues and you'll at least get some numeric output.
Before the loop add
p=zeros(20,1); t=p; % preallocate
Inside the loop then
p=...
needs must become
p(n)=...;
and likewise for t to save the individual values.

4 Comments

i started with t=300
dpb
dpb on 2 May 2014
Edited: dpb on 2 May 2014
Your equations are very unstable--in particular t oscillates wildly and then essentially "blows up". Observe first few iterations...
>> t=300;p=0;
>> n=1; disp([n t p])
while n<6
p=10^((30.59051-8.2*log10(t))+(0.0024804*t)-(3142.31/t));
t=314+((6.324-p)/(6.66*(10^-4)*101.32));
n=n+1;
disp([n t p])
end
1 300 0
2.0000 355.3940 3.5308
3.0000 -360.1268 51.8135
1.0e+18 *
0.0000 -3.4568 + 2.5115i 0.2333 - 0.1695i
5.0000 407.7179 0
1.0e+03 *
0.0060 -4.1697 0.3089
>>
This isn't a Matlab problem; it's in the equations as written...
thank you
So what are the equations intended to represent and where did you obtain them? Looks like a correlation of some sort; are you sure you've coded them correctly and are using proper units to scale t,p initial values within defined range of the correlation is so?

Sign in to comment.

More Answers (1)

You need to initialize your variable t

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

on 1 May 2014

Commented:

dpb
on 3 May 2014

Community Treasure Hunt

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

Start Hunting!