Solving an equation using previously determined variables

1 view (last 30 days)
I am trying to write a code that will calculate various things about a combustion engine. At the beginning I ask the user for certain values. In the code below what I would like to happen is for the equation I am using to solve for Y to use the values for N1 N2 N3 N4 N5 instead of using them as symbols. I had to comment out the lines with N4 and N5 because I cant figure out how to have MatLab let me save the variable as an equation of variables.
What I really want the equation I'm solving for to be is:
2*Y = (N1)*2 + (N3) + (N5)*Y (Solving for Y)
which is equivalent to
2*y = (MC)*2 + (MH/2) + 3.76* Y
I want to plug those equations in so that I get a number answer for Y
When I move the if statement that declares the variables N1-N5 before and take out the "%" I receive an error since there isn't a value for Y yet.
Please let me know of any suggestions you may have.
Thank you
clc
clear
prompt = 'How many moles of H? ';
MH = input(prompt);
prompt = 'How many moles of C? ';
MC = input(prompt);
prompt = 'How many moles of O? ';
MO = input(prompt);
prompt = 'Heating Value of fuel? ';
Hrp = input(prompt);
prompt = 'Temperature of Products? ';
Tp = input(prompt);
prompt = 'Temperature of Reactants? ';
Tr = input(prompt);
prompt = 'Excess fuel or air? Type 1 for fuel or 2 for air. ';
E = input(prompt);
%Ycc Calculation
Ycc = MC + (MH/4) + (MO/2);
%Y Calculation using balancing of O
syms Y N1 N2 N3 N5
eqn = N1 + N2*2 + N3 + N5*2 == Y*2;
Y = solve(eqn,Y);
%N1, N2, N3 Calculations
if (E == 2)
N1 = 0;
N2 = MC;
N3 = (MH/2);
%N4 = (Y - Ycc);
%N5 = 3.76*(Y);
else u = 6;
end

Answers (1)

Walter Roberson
Walter Roberson on 9 Oct 2015
seqn = subs(eqn, {N1, N3, N5}, {MC, MH/2, 3.76});
Y = solve(seqn,Y);
Warning: The expression you wrote out as needing to be solved is not the same as is in your code! You wrote (N5)*Y but your eqn has N5*2 with no Y but with a factor of 2. That leaves Y existing only the the right hand side which leads to a trivial calculation. I would suggest to you that you may have miscoded your eqn .

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!