1

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!

  • You want to solve 4 independent set of equations, each consisting of 8 unknowns and equations. So, `x0` correctly is a `8x4` matrix, but `fun1` should also be an `8x4` matrix. `Vn1` should probably be a row vector (`1x4`) and you should use `x(i, :)` instead of linear indexing `x(i)`. – m7913d May 09 '17 at 18:34
  • It' working. Thank you very much! – Maciek Wajsprych May 09 '17 at 18:52
  • 1
    Maybe you can post your corrected code as an answer and accept it. It may be useful for others. – m7913d May 09 '17 at 19:01
  • 1
    Yes, of course. I was trying to do it as your answer but as I see only I can post it. I wanted to post it as an acknowledgment to you. – Maciek Wajsprych May 09 '17 at 19:10
  • 1
    It's nice of you that you want to acknowledge me. You can upvote my comment or refer to my comment in your answer, but the most important thing is that the solution is available to everyone. I included my comment in your answer, I think you can accept it now. – m7913d May 09 '17 at 19:14
  • 1
    It's says I can accept my answer in 2 days – Maciek Wajsprych May 09 '17 at 19:22
  • 1
    I really hate time limit restrictions. – m7913d May 09 '17 at 19:23
  • I will do it for sure. Thanks again – Maciek Wajsprych May 09 '17 at 19:24

1 Answers1

1

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')
m7913d
  • 10,244
  • 7
  • 28
  • 56