how to assign vpasolve results to a vector?
3 次查看(过去 30 天)
显示 更早的评论
So i did some coding but vpasolve is just giving out single values. How do i assignt those values to a vector?
Here ist the code im wirking with:
clear all
clc
% Parameter
A_1 = 8.23714;
A_2 = 8.19625;
B_1 = 1592.864;
B_2 = 1730.63;
C_1 = 226.184;
C_2 = 233.426;
c1 = 1.701;
c2 = 0.9425;
p = 1022.48; % [mbar]
T = 78.3; % [°C]
% x1 als symbolische variable
syms x1
% Berechnung g1 and g2
g1 = exp((c1.*((1-x1)).^2)./(((1-x1)+(c1/c2).*x1).^2));
g2 = exp((c2.*(x1).^2)./((x1+(c2/c1).*(1-x1)).^2));
for j=1:217
T=T+0.1
% Berechnung p1 and p2
p1 = 10^(A_1-(B_1./(T+C_1)));
p2 = 10^(A_2-(B_2./(T+C_2)));
% Funktion
eqn = (g1.*p1.*x1) + (g2.*p2.*(1-x1)) - p == 0;
% symbolische Lösung der Gleichung
results = vpasolve(eqn, x1)
end
0 个评论
采纳的回答
Dyuman Joshi
2023-7-3
A_1 = 8.23714;
A_2 = 8.19625;
B_1 = 1592.864;
B_2 = 1730.63;
C_1 = 226.184;
C_2 = 233.426;
c1 = 1.701;
c2 = 0.9425;
p = 1022.48; % [mbar]
T = 78.3; % [°C]
% x1 als symbolische variable
syms x1
% Berechnung g1 and g2
g1 = exp((c1.*((1-x1)).^2)./(((1-x1)+(c1/c2).*x1).^2));
g2 = exp((c2.*(x1).^2)./((x1+(c2/c1).*(1-x1)).^2));
%%Number of iteration
n = 217;
%%Preallocate the output array
results = zeros(1,n);
for j=1:n
T=T+0.1;
% Berechnung p1 and p2
p1 = 10^(A_1-(B_1./(T+C_1)));
p2 = 10^(A_2-(B_2./(T+C_2)));
% Funktion
eqn = (g1.*p1.*x1) + (g2.*p2.*(1-x1)) - p == 0;
% symbolische Lösung der Gleichung
%%Store the result in the respective element
results(j) = vpasolve(eqn, x1);
end
%Display the output
results
更多回答(1 个)
Samay Sagar
2023-7-3
You can initialize an empty array and append the results to it. You can implement the same as following :
res_vector=[];
results = vpasolve(eqn, x1);
res_vector=[res_vector results]
1 个评论
Dyuman Joshi
2023-7-3
Dynamically growing arrays is not a good coding practice. It is not efficient.
Refer to Preallocation - "for and while loops that incrementally increase the size of a data structure each time through the loop can adversely affect performance and memory use. Repeatedly resizing arrays often requires MATLAB® to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks. Often, you can improve code execution time by preallocating the maximum amount of space required for the array."
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!