matlabequations

Solving multi equations in matrix form using fsolve (Matlab)


Good morning, I'm trying to solve 8 equations with 8 unknowns using fsolve and want to see how results are changing with one parameter (Vn1). When used it doesn't work correctly - it overwrites score and at the end there is only result with last value of Vn1. Can you help me?

Here is my code:

clear
clc

Vn=100;
Vn1=[10;11;12;13];
Vn3=20;
wn=1;
lambda1=0.1;
lambda3=0.2;
R1=0.99;
R3=0.98;

fun1 = @(x) [(Vn-Vn1+x(1)-x(2));
            x(2)-Vn3+x(3)-x(4);
            x(5)-wn.*(1+lambda1.*R1./(1-lambda1));
            x(6)-x(7).*(1+lambda3.*R3./(1-lambda3));
            x(7).*x(2)-wn.*(Vn-Vn1)-x(5.)*x(1);
            x(8).*x(4)-x(7).*(x(2)-Vn3)-x(6).*x(3);
            x(1)-Vn1+lambda1.*Vn1;
            x(3)-Vn3+lambda3.*Vn3];

x0(8,4)=0;
x = fsolve(fun1,x0);
plot(Vn1,x(8))

Thank you in advance!


Solution

  • As suggested by m7913d in his comment, I want to solve 4 independent sets of equations, each consisting of 8 unknowns and 8 equations. So, x0 correctly is a 8x4 matrix, but fun1 should also be an 8x4 matrix. Therefore, Vn1 should be a row vector (1x4) and I should use x(i, :) instead of linear indexing x(i):

    clear
    clc
    
    Vn=100;
    Vn1=linspace(10,50,100);
    Vn3=20;
    wn=1;
    
    lambda3=0.2;
    lambda1=0.1;
    R1=0.99;
    R3=0.98;
    fun1 = @(x) [(Vn-Vn1+x(1,:)-x(2,:));
                x(2,:)-Vn3+x(3,:)-x(4,:);
                x(5,:)-wn.*(1+lambda1.*R1./(1-lambda1));
                x(6,:)-x(7,:).*(1+lambda3.*R3./(1-lambda3));
                x(7,:).*x(2,:)-wn.*(Vn-Vn1)-x(5,:).*x(1,:);
                x(8,:).*x(4,:)-x(7,:).*(x(2,:)-Vn3)-x(6,:).*x(3,:);
                x(1,:)-Vn1+lambda1.*Vn1;
                x(3,:)-Vn3+lambda3.*Vn3];
    
    x0(8,100)=10;
    x = fsolve(fun1,x0);
    plot(Vn1,x(8,:))
    xlabel('Vn1')
    ylabel('wr')