Clear Filters
Clear Filters

how to assign vpasolve results to a vector?

3 views (last 30 days)
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

Accepted Answer

Dyuman Joshi
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
results = 1×217
0.8657 0.8048 0.7681 0.7386 0.7131 0.6900 0.6688 0.6489 0.6301 0.6122 0.5950 0.5784 0.5624 0.5469 0.5318 0.5171 0.5027 0.4887 0.4750 0.4616 0.4485 0.4357 0.4232 0.4109 0.3989 0.3872 0.3758 0.3647 0.3539 0.3435

More Answers (1)

Samay Sagar
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
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."

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!