I am recieving an error in creating the solution however it still prints a result.

1 回表示 (過去 30 日間)
Lily 2024 年 4 月 21 日
コメント済み: Star Strider 2024 年 4 月 21 日
clc
clear all
close all
%Homework 7 Question 3
Tin=298; %K
Cp=80; %J/(mol K)
CAin=2000; %mol/m^3
k0=10e-7; %m3/(mol min)
Ea_R=5000; %K
rho=1000; %mol/m^3
CA=linspace(2,2000,61);
xA=(CAin-CA)/CAin;
T_sol=zeros(1,length(CA));
k_sol=zeros(1,length(CA));
tau_sol=zeros(1,length(CA));
DHR=-50000; %J/mol exothermic (gets hot)
for i=1:length(CA)
syms T k tau
eqn1=k==k0*exp((-Ea_R)*((1/T)-(1/Tin)));
eqn2=2*k*(CA(i)^2)==(CAin-CA(i))/tau;
eqn3=k*tau*(CA(i))^2*DHR==rho*Cp*(Tin-T);
solution=solve([eqn1,eqn2,eqn3],[T,k,tau]);
T_sol(i)=double(solution.T);
k_sol(i)=double(solution.k);
tau_sol(i)=double(solution.tau);
end
figure(1)
plot(log10(tau_sol),xA,'-bo');
xlabel('log_{10}(\tau, min)')
ylabel('x_A')

サインインしてコメントする。

採用された回答

Star Strider 2024 年 4 月 21 日

In the last iteration of the loop, the results are all empty. One way to avoid the error is to test for at least one field to be empty:
if ~any(structfun(@isempty, solution))
T_sol(i)=double(solution.T);
k_sol(i)=double(solution.k);
tau_sol(i)=double(solution.tau);
end
Try this —
clc
clear all
close all
%Homework 7 Question 3
Tin=298; %K
Cp=80; %J/(mol K)
CAin=2000; %mol/m^3
k0=10e-7; %m3/(mol min)
Ea_R=5000; %K
rho=1000; %mol/m^3
CA=linspace(2,2000,61);
xA=(CAin-CA)/CAin;
T_sol=zeros(1,length(CA));
k_sol=zeros(1,length(CA));
tau_sol=zeros(1,length(CA));
DHR=-50000; %J/mol exothermic (gets hot)
for i=1:length(CA)
syms T k tau
eqn1=k==k0*exp((-Ea_R)*((1/T)-(1/Tin)));
eqn2=2*k*(CA(i)^2)==(CAin-CA(i))/tau;
eqn3=k*tau*(CA(i))^2*DHR==rho*Cp*(Tin-T);
solution=solve([eqn1,eqn2,eqn3],[T,k,tau]);
if ~any(structfun(@isempty, solution))
T_sol(i)=double(solution.T);
k_sol(i)=double(solution.k);
tau_sol(i)=double(solution.tau);
end
end
figure(1)
plot(log10(tau_sol),xA,'-bo');
xlabel('log_{10}(\tau, min)')
ylabel('x_A')
EDIT — (21 Apr 2024 at 20:14)
Changed if condition to use:
structfun(@isempty, solution)
Code otherwise unchanged.
.
2 件のコメントなしを表示なしを非表示
Lily 2024 年 4 月 21 日
Worked perfectly thanks!
Star Strider 2024 年 4 月 21 日
As always, my pleasure!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeChemistry についてさらに検索

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by