How to plot different lengths of vectors?

조회 수: 2 (최근 30일)
asli eylul sert
asli eylul sert 2021년 6월 11일
댓글: asli eylul sert 2021년 6월 12일
Hello, I have this code
I want to plot (T vs yCO) but for some reason I have 250 values from "eqn" but 125 temperature values. So, naturally I am getting the error of different lengths. Can anyone help me?
T = 298:1:422; %Temperature Values
K1 = exp((-deltaHrxn1*1000 + T.*deltaSrxn1.*1000)./(R.*T)); %equilibrium constant for reaction 1
K2 = exp((-deltaHrxn2*1000 + T.*deltaSrxn2.*1000)./(R.*T)); %equilibrium constant for reaction 2
syms ksi1 ksi2
eqn = [(((ksi1)./(1-ksi1-ksi2))./((2-2*ksi1-ksi2)/(4-2*ksi1))^2)==K1, (((1+ksi2).*(2-2*ksi1-ksi2))./((-ksi2).*(1+ksi2)))==K2];
S = arrayfun(@(X)vpasolve(X), eqn, 'uniform', 0);
S = [S{:}];
S.ksi2; %ksi2 = 0 for all values
S.ksi1;
ksi2 = 0;
ksi1 = [S.ksi1];
yCO = ((1 - ksi1)./(4 - 2*ksi1))
  댓글 수: 1
vimal kumar chawda
vimal kumar chawda 2021년 6월 11일
There might be few conditions are as
1- Temp will be same although yCO still getting value and last it will be equal number.
2- you have to take equal number , which you can delete or not considering yco values.
3- Increase the interval of the temperature so that both index will be equal.
Please put the whole code it will never going to be answer in the short time like half code ?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 12일
T = 298:1:422; %Temperature Values
That is a row vector of 125 values
K1 = exp((-deltaHrxn1*1000 + T.*deltaSrxn1.*1000)./(R.*T)); %equilibrium constant for reaction 1
That is a row vector of 125 values
K2 = exp((-deltaHrxn2*1000 + T.*deltaSrxn2.*1000)./(R.*T)); %equilibrium constant for reaction 2
That is a row vector of 125 values
syms ksi1 ksi2
eqn = [(((ksi1)./(1-ksi1-ksi2))./((2-2*ksi1-ksi2)/(4-2*ksi1))^2)==K1, (((1+ksi2).*(2-2*ksi1-ksi2))./((-ksi2).*(1+ksi2)))==K2];
Because K1 is a row vector of 125 values, the first component before the comma is a row vector of 125 values, and because K2 is a row vector of 125 values, the second component is a row vector of 125 values. You [] the two row vectors together to get a row vector of 250 values.
S = arrayfun(@(X)vpasolve(X), eqn, 'uniform', 0);
You arrayfun over the row vector of 250 values. That will cause vpasolve() to be invoked 250 times, each time being given one equation that involves two symbolic variables. vpasolve() will hunt around and find some pair of symbolic values that solves the one equation in two unknowns, and will return a struct for each one.
You need to arrange so that you vpasolve() a pair of equations at a time. For example if you had done
eqn = [(((ksi1)./(1-ksi1-ksi2))./((2-2*ksi1-ksi2)/(4-2*ksi1))^2)==K1; (((1+ksi2).*(2-2*ksi1-ksi2))./((-ksi2).*(1+ksi2)))==K2];
then now eqn would be a 2 x 125 array with the first row being the first of the equations and the second being the second of the equations. Then
arrayfun(@(COL) vpasolve(eqn(:,COL)), 1:size(eqn,2), 'uniform', 0)
  댓글 수: 1
asli eylul sert
asli eylul sert 2021년 6월 12일
Thank you so much. It seems to be working out. I have 2x125 array like you said. But now I have imaginary solutions and my real solutions is always between 0 and 1. How can I put this information into arrayfun so that I only get the real solutions between [0 1] ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 12일
Check your constants, R, deltaHrxn1, deltaHrxn2,...
Note that the solutions might have two components, real and imaginary parts
deltaHrxn1 = 3;
deltaSrxn1 = 3.5;
deltaSrxn2 = 2*pi;
deltaHrxn2 = pi/2;
R = 5;
T = 298:422; %Temperature Values
...
plot(T, real(yCO),'b', T, imag(yCO), 'r'), legend('Real','Imag') %% Works ok

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by