Why does my code take forever to give a result?

1 次查看(过去 30 天)
Hello,
I created this code in order to find the capacities of 10 capacitors for a given circuit. But when I run it, it takes a lot of time to run and after 30 minutes it gives back "Warning : Solutions might be lost." Does anybody know why is that?
%calculates capacitance in series
s = @(varargin) 1/sum(1./[varargin{:}]);
%calculates capacitance in parallell
p = @(varargin) sum([varargin{:}]);
%the system of 10 linear equations
syms c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
eqn1 = p(c1,s(c5,c2),s(c8,c3),s(c9,c4)) == 0.3;
eqn2 = p(c2,s(c5,c1),s(c6,c3),s(c10,c4)) == 0.3;
eqn3 = p(c3,s(c8,c1),s(c6,c2),s(c7,c4)) == 0.3;
eqn4 = p(c4,s(c7,c3),s(c10,c2),s(c9,c1)) == 0.3;
eqn5 = p(c5,s(c1,c2),s(c9,c10),s(c8,c6)) == 0.25;
eqn6 = p(c8,s(c9,c7),s(c1,c3),s(c5,c6)) == 0.25;
eqn7 = p(c9,s(c1,c4),s(c8,c7),s(c5,c10)) == 0.25;
eqn8 = p(c6,s(c10,c7),s(c2,c3),s(c5,c8)) == 0.25;
eqn9 = p(c10,s(c6,c7),s(c5,c9),s(c2,c4)) == 0.25;
eqn10 = p(c7,s(c3,c4),s(c8,c9),s(c6,c10)) == 0.25;
%the values of the 10 capacitors
sol = solve([eqn1, eqn2, eqn3, eqn4, eqn5, eqn6, eqn7, eqn8, eqn9, eqn10],[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10]);
c1Sol = sol.c1;
c2Sol = sol.c2;
c3Sol = sol.c3;
c4Sol = sol.c4;
c5Sol = sol.c5;
c6Sol = sol.c6;
c7Sol = sol.c7;
c8Sol = sol.c8;
c9Sol = sol.c9;
c10Sol=sol.c10;
  3 个评论
bransa
bransa 2022-5-20
you can use Run & Time function in the Editor (in the drop down of the Run button) or wrap sections of your code in tic...toc to check how long each step is taking.

请先登录,再进行评论。

回答(1 个)

Abhiram V. P. Premakumar
Does you equation have feasible solutions?
I checked with a simple feasible problem, and it did give the correct answer.
Please check that as a first step.
clc;clear all;
%calculates capacitance in series
s = @(varargin) 1/sum(1./[varargin{:}]);
%calculates capacitance in parallell
p = @(varargin) sum([varargin{:}]);
%the system of 2 linear equations
syms c1 c2
eqn1 = p(c1,c2) == 2;
eqn2 = s(c1,c2) == 0.5;
sol = solve([eqn1, eqn2],[c1, c2]);
c1Sol = sol.c1
c1Sol = 
1
c2Sol = sol.c2
c2Sol = 
1

类别

Help CenterFile Exchange 中查找有关 Functions 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by