Solving Equations by using Iteration Process

1 次查看(过去 30 天)
Hello,
I am trying to find a quicker way to solve 3 equations with 3 unknowns. So far, I used the function named "solve," but it takes too much time solving the problem. To do this provess it takes 20 minutes. Any suggestions on how to make it faster, or may be using a while loop? How can I solve using a while loop? Thank you, Here is the code below:
for i = 1:8760; % Input range of time, hours
g=i
% Given 3 equations, and 3 unkowns, it calculates the wet bulb
% temperature, in C
t = DryBulb(i,1)-((DryBulb(i,1)-DewPoint(i,1))/3);
if DryBulb(i,1) <= 0
syms t p_ws_star w_star;
[pws t_wb wstar] = solve (w(i,1) - ((2830 - 0.24*t)*w_star - 1.006*(DryBulb(i,1)- t))/(2830 + 1.86*DryBulb(i,1) - 2.1*t), p_ws_star - exp(C_1/(t+273.15) + C_2 + C_3*(t+273.15) + C_4*(t+273.15).^2 + C_5*(t+273.15).^3 + C_6*(t+273.15).^4 + C_7*log(t+273.15))/1000, w_star - 0.621945*p_ws_star/(Press(i,1) - p_ws_star));
WB = double(t_wb);
elseif DryBulb(i,1) > 0
syms t p_ws_star w_star;
[pws t_wb wstar] = solve (w(i,1) - ((2501 - 2.326*t)*w_star - 1.006*(DryBulb(i,1) - t))/(2501 + 1.86*DryBulb(i,1) - 4.186*t), p_ws_star - exp(C_8/(t+273.15) + C_9 + C_10*(t+273.15) + C_11*(t+273.15).^2 + C_12*(t+273.15).^3 + C_13*log(t+273.15))/1000, w_star - 0.621945*p_ws_star/(Press(i,1) - p_ws_star));
WB = double(t_wb);
end
% Calculates the wet bulb temperature, in C
T_wb = [T_wb; WB];
end

回答(1 个)

arushi
arushi 2024-8-16
Hi X,
To improve the efficiency of solving a system of equations in MATLAB, especially when using the solve function within a loop, you may consider the following strategies:
1. Use Numerical Solvers Instead of Symbolic Solvers
The solve function is symbolic and can be slow for large numbers of iterations. Instead, use numerical solvers like fsolve from the Optimization Toolbox, which is generally faster for numeric solutions.
2. Precompute Constant Values
If there are any calculations that do not depend on the loop index i, compute them outside the loop to save time.
3. Vectorize Your Code
Where possible, vectorize your operations to avoid loop overhead. However, in this case, since each iteration depends on the previous result, full vectorization might not be feasible.
4. Use a parfor Loop for Parallel Execution
If you have access to the Parallel Computing Toolbox, you can use parfor to parallelize the loop execution, provided the iterations are independent.
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by