why does program show wrong results for my code?

%This program calculate K-factor from Standing Equation
%mixture contain:
%1=C1,2=C2,3=C3,4=i-C4,5=n-C4,6=i,C5,7=n,C5,8=C6,H2S=9,10=CO2,11=N2,12=C7+
clear
clc
clf
disp('calculating K-factore from Wilson equation')
disp('')
disp('mixture contain C1,C2,C3,i-C4,n-C4,i-C5,n-C5,C6,H2S,CO2,N2,C7+')
%input
P=input('pressure(psia)=');
T=input ('Temperature(R)=');
M_C7p=input('Molecular weight of C7+=');
gama=input('Relative density of C7+=');
n=7.30+.0075*(T-460)+.0016*P;
bc7p=1.13+324*n-4.256*n^2;
Tb_c7p=301+59.85*n-.971*n^2;
Tb=[94 303 416.25 471 491 542 557 610 331 194 109 Tb_c7p];
Tb=[Tb Tb_c7p];
b=[300 1145 1799 2037 2153 2368 2480 2738 1136 652 470 bc7p];
for i=1:11
F(i)=b(i)*((1/Tb(i))-(1/T));
a=1.2+(.00045*P)+((15*10^-8)*P^2);
c=.89-(.00017*P)-(35*10^-8)*P^2;
K(i)=(1/P)*(10^(a+c*F(i)));
end
fprintf('K(c1)=%f\n K(c2)=%f\n K(c3)=%f\n K(i-c4)=%f\n',K(1),K(2),K(3),K(4))
fprintf('K(n-c4)=%f\n K(i-c5)=%f\n K(n-c5)=%f\n K(c6)=%f\n',K(5),K(6),K(7),K(8))
fprintf('K(co2)=%f\n K(N2)=%f\n K(c7+)=%f\n',K(9),K(10),K(11))

11 Comments

Jan
Jan on 3 Jun 2018
Edited: Jan on 3 Jun 2018
We cannot know which inputs you have, which output you get, which output you expect and if there is an error in the code or in the maths. Therefore this question cannot be answered yet.
Please post only one thread per question.
First of all thank you for your respond. where shall I write my input &output...?I mean in which part(Body,Tags or products)?
The inputs and a description of the expected behavior belong to the body of the question. If the data are small, post them as text. Larger data can be attached as MAT file. Using input is very inconvenient, because it requires to type the data again. Prefer to write a function and define the inputs as variables. This allows for an efficient debugging.
Check if your formulas are correct.
What shall I check? It just gives me wrong outputs
"What shall I check?"
Everything, starting from the first line.
There is no magic to debugging code, just hard slog. You need to go through your code and check each line: compare its input and output values against what you expect to get, calculating the expected values using some other tool or method (on paper is fine too!). Debugging is not fast, nor is it particularly exciting, but it is what you will have to do if you want to fix your code.
What is a wrong output? If you fprintf 1e-10 as floating point number matlab will tell you it is 0.
I attached my data.If u enter the inputs,it gets u wrong outputs.It must get my expectatin outputs but it doesn't
You have posted the values as PDF. This is inconvenient and prone to errors when I juggle with different documents while trying to reproduce your results.
There are 3 possible causes:
  1. There is a mistake in your formulas.
  2. There is a bug in the implementation in Matlab.
  3. The method, you used to obtain the expected results, failed.
All we see is the failing code and of course we do expect that Matlab replies exactly what the code instructs it to do. Therefore there is still no chance, that we can help you to find the reason for the deviation between the results and your expectations. It is like when I post:
% My code:
a = 3; b = 5;
c = a + b;
>> 8
But I expect 17 - where is the error?
Do you see it? Please edit the question. Provide the inputs as code and post which equation you want to solve.
I attached my data in (.m) form and my equation as picture.I'm sure of writing my equation correctly.My question is chosen from a book that I don't know what it is by my teacher but the question had these outputs as result that I attached.So my code must get these outputs to me.I'd be appreciated if u check it again...

Sign in to comment.

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!