1

I have a system of two equations and I need Matlab to solve for a certain variable. The problem is the variable I need is inside an expression, and trig functions. I wrote the following code:

function [ V1, V2 ] = find_voltages( w1, l1, d, w2, G1, G2, m, v, e, h, a, x)

k1 = sqrt((2*V1*e)/(G1^2*m*v^2));
k2 = sqrt((2*V2*e)/(G2^2*m*v^2));

A = h + l1*a;
b = -A*k1*sin(k1*w1) + a*cos(k1*w1);
B = A*cos(k1*w1) + (a/k1)*sin(k1*w1);
C = B + a*b;
c = C*k2*sinh(k2*w2) + b*cosh(k2*w2);
D = C*cosh(k2*w2) + (b/k2)*sinh(k2*w2);

bd = A*k1*sinh(k1*w1) + a*cosh(k1*w1);
Bd = A*cosh(k1*w1) + (a/k1)*sinh(k1*w1);
Cd = Bd + a*bd;
cd = -Cd*k2*sin(k2*w2) + bd*cos(k2*w2);
Dd = Cd*cos(k2*w2) + (bd/k2)*sin(k2*w2);

fsolve([c*(x-(l1+w1+d+w2)) + D == 0, cd*(x-(l1+w1+d+w2)) + Dd == 0], [V1,V2])

end

and got an error because V1 and V2 are not defined. They are part of an expression, and need to be solved for. Is there a way to do this? Also, is it a problem that the functions I put as arguments to solve are conglomerates of the smaller equations above them?

Valid values: 
Drift space 1 (l1): 0.11
Quad 1 length (w1): 0.11
Quad 2 length (w2): 0.048
Separation (d): 0.014
Radius of Separation 1 (G1):    0.016
Radius of Separation 2 (G2):    0.01
Voltage 1 (V1): -588.5
Voltage 2 (V2): 418
Kinetic Energy in eV:   15000
Mass (m)    9.109E-31
Kinetic Energy in Joules (K):   2.4E-15
Velocity (v):   72591415.94
Charge on an Electron (e):  1.602E-19

k1^2=(2*V1*e)/(G1^2*m*v^2): 153.4467773
k2^2=(2*V2*e)/(G2^2*m*v^2): 279.015

1 Answers1

1

First start by re-writing your function as an expression which returns the extent to which your function(s) fail to hold for some valid guess for [V1,V2]. E.g.,

function gap = voltage_eqn(V, w1, l1, d, w2, G1, G2, m, v, e, h, a, x)
    V1 = V(1) ;
    V2 = V(2) ;

    k1 = sqrt((2*V1*e)/(G1^2*m*v^2));
    k2 = sqrt((2*V2*e)/(G2^2*m*v^2));

    A = h + l1*a;
    b = -A*k1*sin(k1*w1) + a*cos(k1*w1);
    B = A*cos(k1*w1) + (a/k1)*sin(k1*w1);
    C = B + a*b;
    c = C*k2*sinh(k2*w2) + b*cosh(k2*w2);
    D = C*cosh(k2*w2) + (b/k2)*sinh(k2*w2);

    bd = A*k1*sinh(k1*w1) + a*cosh(k1*w1);
    Bd = A*cosh(k1*w1) + (a/k1)*sinh(k1*w1);
    Cd = Bd + a*bd;
    cd = -Cd*k2*sin(k2*w2) + bd*cos(k2*w2);
    Dd = Cd*cos(k2*w2) + (bd/k2)*sin(k2*w2);

    gap(2) = c*(x-(l1+w1+d+w2)) + D ;
    gap(1) = cd*(x-(l1+w1+d+w2)) + Dd ;

end

Then call fsolve from some initial V0:

Vf = fsolve(@(V) voltage_eqn(V,  w1, l1, d, w2, G1, G2, q, m, v, e, h, a, x), V0) ;
  • I am not sure what you mean by "First start by re-writing your function as an expression which returns the extent to which your function(s) fail to hold for some valid guess for [V1,V2]. E.g" Do I have to guess V1 and V2? And I need an initial V0? There are two equations, and two unknowns, so It doesn't seem like I should have to put any initial conditions for V1 and V2. – pictorexcrucia Jul 23 '15 at 19:39
  • Welcome to numerical computing. If you want to use any of the algorithms in `fsolve`, which is probably the ideal place to start, you need to provide an initial guess. If you have difficulty finding reasonable initial guesses (reasonable in the sense that you are able to obtain a solution from those initial conditions) then there are other methods which may be helpful. – transversality condition Jul 23 '15 at 19:41
  • If i were to do this by hand, I would use Taylor polynomials to approximate the trig functions and solve for V1 and V2 that way. Can I make Matlab do something similar? – pictorexcrucia Jul 23 '15 at 19:47
  • Also if i am going to use the way you suggested in your answer, what is V again? I understand V0 is an initial value. – pictorexcrucia Jul 23 '15 at 19:47
  • Well with `fsolve` MATLAB will construct an approximate quadratic local model of the norm of your function and then choose steps to minimize this norm using either trust region or line search backtracking algorithms. If you look up Levenberg-Marquardt and trust-region optimization in Wikipedia you can find more information. – transversality condition Jul 23 '15 at 19:51
  • @pictorexcrucia V is just the concatenation of V1 and V2 to conform with the required syntax for `fsolve`. It requires you to specify your problem in terms of a single vector of unknowns. – transversality condition Jul 23 '15 at 19:53