Search code examples
matlabsolver

Solving nonlinear system with fsolve with multiple inputs in matlab


I am trying to solve a system of six nonlinear equations using fsolve (see below). There are additional parameters in my equations that I would like to be able to input into the system or change the functionality without having to go into my function and manually editing them each time.

They are F, rho, and A. Right now I have set them all to 1, and I am able to get a solution so at least the script is running correctly.

function f = pressXmanifold(x)
    F = [1,1,1];
    rho = 1;
    A = 1;
    f(1) = x(1)-x(3) - ...
        1/2*rho*(x(4)/F(1))^2*...
        A*(x(4)/x(6)*F(3)/F(1))^(-2)*...
        (1+(F(3)/F(1))^2+3*(F(3)/F(1))^2*((x(4)/x(6))^2-(x(4)/x(6))));
    f(2) = x(2)-x(3) - ...
        1/2*rho*(x(5)/F(2))^2*...
        A*(x(5)/x(6)*F(3)/F(2))^(-2)*...
        (1+(F(3)/F(2))^2+3*(F(3)/F(2))^2*((x(5)/x(6))^2-(x(5)/x(6))));
    f(3) = x(1) - x(3);
    f(4) = x(2) - x(3);
    f(5) = x(4) - x(5);
    f(6) = x(4)+x(5) - x(6);
end

For F I would want to be able to input any 1 x 3 array into my function. For rho I would want to input a value as well. For A, it would have to change with the iterations based on the following criteria (given as an if statement).

if F(3)/F(1) <= 0.35 && x(3)/x(2) <= 1
    A = 1;
elseif F(3)/F(1) > 0.35 && x(3)/x(2) <= 0.4
    A = 0.9*(1-x(3)/x(2));
elseif F(3)/F(1) > 0.35 && x(3)/x(2) > 0.4
    A = 0.55;
end

For now I've only tried dealing with the first two parameters. f = pressXmanifold(x,F,rho) and removing the first two lines in my function. But when I try to follow the instructions on the MathWorks site

options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);
fun = @pressXmanifold;
x0 = [1,1,1,1,1,1];
x = fsolve(fun,x0,F,rho,options)

Unable to perform assignment because dot indexing is not supported for variables of this type. Error in createOptionFeedback (line 33) options.(stopTestOptions{k}) = []; Error in prepareOptionsForSolver (line 57) optionFeedback = createOptionFeedback(options); Error in fsolve (line 157) [options, optionFeedback] = prepareOptionsForSolver(options, 'fsolve');


Solution

  • Rewrite pressXmanifold including F and rho as input

    function f = pressXmanifold(x1, x2, x3, x4, x5, x6, F, rho)
       if F(3)/F(1) <= 0.35 && x3/x2 <= 1
            A = 1;
        elseif F(3)/F(1) > 0.35 && x3/x2 <= 0.4
            A = 0.9*(1-x3/x2);
        elseif F(3)/F(1) > 0.35 && x3/x2 > 0.4
            A = 0.55;
        end
        f(1) = x1-x3 - ...
            1/2*rho*(x4/F(1))^2*...
            A*(x4/x6*F(3)/F(1))^(-2)*...
            (1+(F(3)/F(1))^2+3*(F(3)/F(1))^2*((x4/x6)^2-(x4/x6)));
        f(2) = x2-x3 - ...
            1/2*rho*(x5/F(2))^2*...
            A*(x5/x6*F(3)/F(2))^(-2)*...
            (1+(F(3)/F(2))^2+3*(F(3)/F(2))^2*((x5/x6)^2-(x5/x6)));
        f(3) = x1 - x3;
        f(4) = x2 - x3;
        f(5) = x4 - x5;
        f(6) = x4+x5 - x6;
    end
    

    Then define F and rho

    rho = 1;
    F =[1,1,1];
    fun = @(x)pressXmanifold(x(1), x(2), x(3), x(4), x(5), x(6), F, rho);
    

    Finally

    options = optimoptions('fsolve','Display','none','PlotFcn',...
        @optimplotfirstorderopt);
    x0 = [1,1,1,1,1,1];
    x = fsolve(fun,x0,options)