Home > Software design >  Matlab: Having trouble making my solver print solutions
Matlab: Having trouble making my solver print solutions

Time:01-19

I wrote this code to solve a system of equations related to a circuit. I'm having trouble getting it to print the outputs.

I was writing this as a function. I'd like to be able to specify a value for C1 and call the function, **but I'd like to have the function print values before I mess with that. ** I'm a novice with matlab, so any explainations you can provide as to why this isn't working would be greatly appreciated!

CODE:

f0 = 1200;
Q = 3;
W0 = 2*pi*f0;
TpdB = -10.5;
Tp = 10^(TpdB/20);
C1 = 680e-9;
syms R1 R2 C2 positive;
sol = [sC2,sR1,sR2];
[sR1,sR2,sC2] = vpasolve(W0==1/sqrt(C1*C2*R1*R2),...
Q == 1/(sqrt((C2*R2)/(C1*R1))*sqrt((C1*R1)/(C2*R2))*sqrt((C1*R2)/(C2*R1))),...
Tp == Q*(sqrt((C1*R2)/(C2*R1))), [R1,R2,C2]);
fprintf('%e %e %e',sol);

Calling this function results in MATLAB calling the function and not printing the outputs.

CodePudding user response:

you have 3 variables to solve R1 C1 C2 but only 2 independent equations.

The 1st equation W0=.. doesn't really help because it brings in a 4th parameter W0 that is not really needed to solve for R1 C1 C2.

vpasolve and for the case any solver is going to need N independent equations to solve N variables.

So you can either

1. still solve with one of variables left as parameter, here you have to choose which one (left as parameter)

or

2. define a 3rd independent equation and then solve.

3. Any chance for readers to see the actual circuit diagram connecting all components?

If so please attach it to the question.

Defining the network with a concise circuit diagram defines in turn all needed equations.

CodePudding user response:

try this correction to understand how to use variables in MATLAB :

f0 = 120;
0;
Q = 3;
W0 = 2*pi*f0;
TpdB = -10.5;
Tp = 10^(TpdB/20);
C1 = 680e-9;
syms R1 R2 C2;

eq1 = W0==1/sqrt(C1*C2*R1*R2);
eq2 = Q == 1/(sqrt((C1*R2)/(C2*R1)));
eq3 = Tp == Q*(sqrt((C1*R2)/(C2*R1)));

[sR1,sR2,sC2] = vpasolve([eq1,eq2,eq3], [R1,R2,C2]);
sol = [sC2,sR1,sR2];
fprintf('sol : %e %e %e ',sol);
% No solutions found by this function vpasolve()

disp('No solutions')
  
[sR1,sR2,sC2] = vpasolve(eq1, [R1,R2,C2]);
sol = [sC2,sR1,sR2];
fprintf(' \n sol for eq1 : %e %e %e \n',sol);
% one solution is found for the equation 1



[sR1,sR2,sC2] = vpasolve(eq1, [R1,R2,C2]);
sol = [sC2,sR1,sR2];
fprintf(' \n sol for eq2 : %e %e %e \n',sol);
% one solution is found for equation 2


[sR1,sR2,sC2] = vpasolve(eq1, [R1,R2,C2]);
sol = [sC2,sR1,sR2];
fprintf(' \n sol for eq3 : %e %e %e \n',sol);
% one solution is found for equation 3

please note that :

  1. The variable sol should be declared after calling the vpasolve() function
  2. The vpasolve() function didn't find any solutions for your system of equations
  • Related