how to assign vpasolve results to a vector?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dyuman Joshi
on 3 Jul 2023
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
More Answers (1)
Samay Sagar
on 3 Jul 2023
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 Comment
Dyuman Joshi
on 3 Jul 2023
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."
See Also
Categories
Find more on Loops and Conditional Statements 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!