필터 지우기
필터 지우기

Why does my code take forever to give a result?

조회 수: 1 (최근 30일)
Enzo Berberi
Enzo Berberi 2022년 5월 20일
답변: Abhiram V. P. Premakumar 2022년 5월 20일
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
Enzo Berberi
Enzo Berberi 2022년 5월 20일
normally it's a 10x10
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
Abhiram V. P. Premakumar 2022년 5월 20일
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