Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Solving simultaneous non linear equations

Subject: Solving simultaneous non linear equations

From: Yehia

Date: 19 May, 2009 13:19:02

Message: 1 of 14

hiii
i m personally interested in the field of Power electronics and i want to implement what is called SHE (Selective Harmonic Elimination) technique in multilevel inverters (DC - AC), this technique is based upon equating all equations expressing harmonics to be eliminated to zero, in other words one would have (N) equations in (N) unknowns ... the problem is that these equations are non-linear ... something like :

cos A + cos B + cos C = M (M is a known constant)
cos 3A + cos 3B + cos 3C = 0 (3rd)
cos 5A + cos 5B + cos 5C = 0 (5th)

fitting these three above equations will get the values of A, B, C which are the firing times of electronic switches to remove 3rd and 5th harmonics.

so my main problem in the project is to solve the set of non linear equations of the form:

Cos x(1) + Cos x(2) + ....... Cos x(n) = M .........................eq(1)
Cos 3x(1) + Cos 3x(2) + ....... Cos 3x(n) = 0 .........................eq(2)
Cos 5x(1) + Cos 5x(2) + ....... Cos 5x(n) = 0 .........................eq(3).
.
.
Cos (n-2)x(1) + Cos (n-2)x(2) + ....... Cos (n-2)x(n) = 0 .........................eq(n)

is there any algorthim or a method i can use in matlab to solve this system of equations .. i tried myself to solve them by hand but exceeding 3 equations i find it so somplex... any suggestions ????

thanks

Subject: Solving simultaneous non linear equations

From: Alan Weiss

Date: 19 May, 2009 13:34:34

Message: 2 of 14

Take a look at fsolve from the Optimization Toolbox:

http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/fsolve.html
function reference page

http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nq6.html
examples

Alan Weiss
MATLAB mathematical toolbox documentation

Yehia wrote:
> hiii
> i m personally interested in the field of Power electronics and i want to implement what is called SHE (Selective Harmonic Elimination) technique in multilevel inverters (DC - AC), this technique is based upon equating all equations expressing harmonics to be eliminated to zero, in other words one would have (N) equations in (N) unknowns ... the problem is that these equations are non-linear ... something like :
>
> cos A + cos B + cos C = M (M is a known constant)
> cos 3A + cos 3B + cos 3C = 0 (3rd)
> cos 5A + cos 5B + cos 5C = 0 (5th)
>
> fitting these three above equations will get the values of A, B, C which are the firing times of electronic switches to remove 3rd and 5th harmonics.
>
> so my main problem in the project is to solve the set of non linear equations of the form:
>
> Cos x(1) + Cos x(2) + ....... Cos x(n) = M .........................eq(1)
> Cos 3x(1) + Cos 3x(2) + ....... Cos 3x(n) = 0 .........................eq(2)
> Cos 5x(1) + Cos 5x(2) + ....... Cos 5x(n) = 0 .........................eq(3).
> .
> .
> Cos (n-2)x(1) + Cos (n-2)x(2) + ....... Cos (n-2)x(n) = 0 .........................eq(n)
>
> is there any algorthim or a method i can use in matlab to solve this system of equations .. i tried myself to solve them by hand but exceeding 3 equations i find it so somplex... any suggestions ????
>
> thanks

Subject: Solving simultaneous non linear equations

From: Yehia

Date: 19 May, 2009 19:54:01

Message: 3 of 14

hii
i m not much familiar with using matlab in solving equations so it was kinda difficult for me to write the correct code ... i tried the optimization toolbox with no use .. so if anyone has got any ideas or clues to start it please tell me ..


thanks in advance

 

Subject: Solving simultaneous non linear equations

From: Miroslav Balda

Date: 20 May, 2009 20:59:03

Message: 4 of 14

"Yehia " <Spartora@hotmail.com> wrote in message <guv2op$6f7$1@fred.mathworks.com>...
> hii
> i m not much familiar with using matlab in solving equations so it was kinda difficult for me to write the correct code ... i tried the optimization toolbox with no use .. so if anyone has got any ideas or clues to start it please tell me ..
>
>
> thanks in advance

Hi,
I see that there is no better answer in the conference to your question. So, there is one solution, which solves the problem approximately:

% yehia.m 2009-05-19
%%%%%%%%%%%%%%%%%%%%%%%
% Cos x(1) + Cos x(2) + ... + Cos x(n) = M % .................eq(1)
% Cos 3x(1) + Cos 3x(2) + ... + Cos 3x(n) = 0 % .................eq(2)
% Cos 5x(1) + Cos 5x(2) + ... + Cos 5x(n) = 0 % .................eq(3)
% .
% Cos (2n-1)x(1) + Cos (2n-1)x(2) + ... + Cos (2n-1)x(n) = 0 % ..eq(n)

n = inp('n',4); % FEX Id 9033
while 1
    M = inp('M',1);
    if abs(M)<n, break, end
end

y = zeros(1,n);
y(1) = M;
h = (1:2:2*n); % harmonics
res = @(x) (sum(cos(x*h))-y)';

[x,ssq,cnt] = LMFnlsq(res,rand(n,1)*2*pi,'Display',-1)
X = x-fix(x/(2*pi))*2*pi

You may input number of equations n and the number M from a keyboard. Default values are n=4 and M=1. res is an anonymous function calculating residuals of your n equations. y is the vector of right-hand sides.

The function LMFnlsq solves the system of nonlinear equations in the least-squares sense starting from the random vector x0, posessessed of the elements, which are from the interval (0,2pi). The last line of the code returns a solution X from the same interval of values. A valid solution is that with cnt>2. The script displays all intermediate results enabling to see how the iteration process converges.

You have to run it in repetition and find the best solution (with lowest ssq). Should it be done automatically, the last-but-one line could be replaced by

S = 1e10;
for k = 1: 100
    [x,ssq,cnt] = LMFnlsq(res,rand(n,1)*2*pi);
    if ssq<S
        S = ssq;
        X = x;
    end
end
x = res(X);
ssq =

Maybe that better solution exists, however, I don't know it.
Hope it helps.
Best regards

Mira

Subject: Solving simultaneous non linear equations

From: Yehia

Date: 18 Jun, 2009 13:34:01

Message: 5 of 14

hello
well i geuss this doesn't work cuz the func. lmfnlsq is undefined or unexisting function

Subject: Solving simultaneous non linear equations

From: Miroslav Balda

Date: 18 Jun, 2009 20:25:02

Message: 6 of 14

"Yehia " <Spartora@hotmail.com> wrote in message <h1dfo9$g9b$1@fred.mathworks.com>...
> hello
> well i geuss this doesn't work cuz the func. lmfnlsq is undefined or unexisting function

Hi,

Reasons that you have got an arror might be two:

1. You didn't download the function LMFnlsq.m from File Exchange. You could find it under
http://www.mathworks.com/matlabcentral/fileexchange/17534

2. You didn't apply LMFnlsq, but lmfnlsq. Attention! MATLAB is case sensitive!

Hope it helps.

Mira

Subject: Solving simultaneous non linear equations

From: Yehia

Date: 18 Jun, 2009 22:44:02

Message: 7 of 14

hii
thanks brother for ur help .. it started to work now and i m trying to find a solution
and i have a question too
when i change the 2 lines

[x,ssq,cnt] = LMFnlsq(res,rand(n,1)*2*pi,'Display',-1)
X = x-fix(x/(2*pi))*2*pi

to

[x,ssq,cnt] = LMFnlsq(res,rand(n,1)*(pi/2),'Display',-1);
X = x-fix(x/(pi/2))*(pi/2);

means that the interval is decreased from [0,2pi] to [0,pi/2] , correct ?

Subject: Solving simultaneous non linear equations

From: Miroslav Balda

Date: 21 Jun, 2009 19:39:01

Message: 8 of 14

"Yehia " <Spartora@hotmail.com> wrote in message <h1efvi$7gn$1@fred.mathworks.com>...
> hii
> thanks brother for ur help .. it started to work now and i m trying to find a solution
> and i have a question too
> when i change the 2 lines
>
> [x,ssq,cnt] = LMFnlsq(res,rand(n,1)*2*pi,'Display',-1)
> X = x-fix(x/(2*pi))*2*pi
>
> to
>
> [x,ssq,cnt] = LMFnlsq(res,rand(n,1)*(pi/2),'Display',-1);
> X = x-fix(x/(pi/2))*(pi/2);
>
> means that the interval is decreased from [0,2pi] to [0,pi/2] , correct ?

Hi,
I am affraid, not. Try it.
Mira

Subject: Solving simultaneous non linear equations

From: Llullaillaco Bernhardt

Date: 17 Sep, 2009 03:20:42

Message: 9 of 14


>%%%%%%%%%%%%%%%%%%%%%%%
>% Cos x(1) + Cos x(2) + ... + Cos x(n) = M % .................eq(1)
>% Cos 3x(1) + Cos 3x(2) + ... + Cos 3x(n) = 0 % .................eq(2)
>% Cos 5x(1) + Cos 5x(2) + ... + Cos 5x(n) = 0 % .................eq(3)
>% ..
>% Cos (2n-1)x(1) + Cos (2n-1)x(2) + ... + Cos (2n-1)x(n) = 0 % ..eq(n)
>y = zeros(1,n);
>y(1) = M;
>h = (1:2:2*n); % harmonics
>res = @(x) (sum(cos(x*h))-y)';

Dear Sirs:
I have downloaded LMFnlsq function, the same equations just mentioned above. As it didn?t work, I realized that the problem was my old Matlab version (6.5, release 13), which can?t implement anonymous functions or handles.

The problem that I have:

- implement the sentence: res = @(x) (sum(cos(x*h))-y)';
in an .m file. How I define x, without handles? "Inline" built-in function didn?t work, or I dont know use it.


Many thanks in advance

Subject: Solving simultaneous non linear equations

From: Bert Bern

Date: 17 Sep, 2009 16:15:20

Message: 10 of 14

Don?t burn your brains. I've worked out the solution, though I'm almost sure that new question will arise.

"Llullaillaco Bernhardt" <realberlis@yahoo.com> wrote in message <h8s9ua$qh9$1@fred.mathworks.com>...
>
> >%%%%%%%%%%%%%%%%%%%%%%%
> >% Cos x(1) + Cos x(2) + ... + Cos x(n) = M % .................eq(1)
> >% Cos 3x(1) + Cos 3x(2) + ... + Cos 3x(n) = 0 % .................eq(2)
> >% Cos 5x(1) + Cos 5x(2) + ... + Cos 5x(n) = 0 % .................eq(3)
> >% ..
> >% Cos (2n-1)x(1) + Cos (2n-1)x(2) + ... + Cos (2n-1)x(n) = 0 % ..eq(n)
> >y = zeros(1,n);
> >y(1) = M;
> >h = (1:2:2*n); % harmonics
> >res = @(x) (sum(cos(x*h))-y)';
>
> Dear Sirs:
> I have downloaded LMFnlsq function, the same equations just mentioned above. As it didn?t work, I realized that the problem was my old Matlab version (6.5, release 13), which can?t implement anonymous functions or handles.
>
> The problem that I have:
>
> - implement the sentence: res = @(x) (sum(cos(x*h))-y)';
> in an .m file. How I define x, without handles? "Inline" built-in function didn?t work, or I dont know use it.
>
>
> Many thanks in advance

Subject: Solving simultaneous non linear equations

From: mazen

Date: 27 Jun, 2013 09:25:06

Message: 11 of 14

 Dear Miroslav Balda

 I have seen the solution for the following problem which you solved in 2009


% yehia.m 2009-05-19
%%%%%%%%%%%%%%%%%%%%%%%
% Cos x(1) + Cos x(2) + ... + Cos x(n) = M % .................eq(1)
% Cos 3x(1) + Cos 3x(2) + ... + Cos 3x(n) = 0 % .................eq(2)
% Cos 5x(1) + Cos 5x(2) + ... + Cos 5x(n) = 0 % .................eq(3)
% .
% Cos (2n-1)x(1) + Cos (2n-1)x(2) + ... + Cos (2n-1)x(n) = 0 % ..eq(n)

n = inp('n',4); % FEX Id 9033
while 1
    M = inp('M',1);
    if abs(M)<n, break, end
end

y = zeros(1,n);
y(1) = M;
h = (1:2:2*n); % harmonics
res = @(x) (sum(cos(x*h))-y)';

[x,ssq,cnt] = LMFnlsq(res,rand(n,1)*2*pi,'Display',-1)
X = x-fix(x/(2*pi))*2*pi

I have similar equations which need to be solved but a bit different

Cos x(1) - Cos x(2) + Cos x(3)- Cos x(4) + Cos x(5)........ Cos x(n) = M % .................eq(1)
Cos 3x(1) - Cos 3x(2) + Cos 3x(3) - Cos 3x(4) +Cos 3x(5) ........... Cos 3x(n) = 0 % .................eq(2)
 Cos 5x(1) - Cos 5x(2) +Cos 5x(3) - Cos 5x(4) + Cos 5x(5) ........... Cos 5x(n) = 0 % .................eq(3)
 
-1 [ -1^n [Cos (2n-1)x(1) + Cos (2n-1)x(2) + ... + Cos (2n-1)x(n)]] = 0 % ..eq(n) so the sign is always changing starting form + and then - and so on


I have tried to do the following but it gives wrong solutions ;
res = @(x) (-1*sum((-1)^(n)*cos(x*h))-y)';

So would you please help and tell me how to solve such en equations ??

Thanks in advance
Mazen Alzatari
 

Subject: Solving simultaneous non linear equations

From: Timothy Siahaan

Date: 28 Jun, 2013 14:08:07

Message: 12 of 14

Hi,

You can use multivariable Newton-Raphson method.
Just read this link
http://faculty.washington.edu/finlayso/ebook/algebraic/NR/NRmult.htm
and apply it to your case.

Hopefully it will help.

Good luck.

"Yehia" wrote in message <guubk6$k0m$1@fred.mathworks.com>...
> hiii
> i m personally interested in the field of Power electronics and i want to implement what is called SHE (Selective Harmonic Elimination) technique in multilevel inverters (DC - AC), this technique is based upon equating all equations expressing harmonics to be eliminated to zero, in other words one would have (N) equations in (N) unknowns ... the problem is that these equations are non-linear ... something like :
>
> cos A + cos B + cos C = M (M is a known constant)
> cos 3A + cos 3B + cos 3C = 0 (3rd)
> cos 5A + cos 5B + cos 5C = 0 (5th)
>
> fitting these three above equations will get the values of A, B, C which are the firing times of electronic switches to remove 3rd and 5th harmonics.
>
> so my main problem in the project is to solve the set of non linear equations of the form:
>
> Cos x(1) + Cos x(2) + ....... Cos x(n) = M .........................eq(1)
> Cos 3x(1) + Cos 3x(2) + ....... Cos 3x(n) = 0 .........................eq(2)
> Cos 5x(1) + Cos 5x(2) + ....... Cos 5x(n) = 0 .........................eq(3).
> .
> .
> Cos (n-2)x(1) + Cos (n-2)x(2) + ....... Cos (n-2)x(n) = 0 .........................eq(n)
>
> is there any algorthim or a method i can use in matlab to solve this system of equations .. i tried myself to solve them by hand but exceeding 3 equations i find it so somplex... any suggestions ????
>
> thanks

Subject: Solving simultaneous non linear equations

From: mazen

Date: 1 Jul, 2013 15:43:11

Message: 13 of 14

"Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message <kqk5c7$lf1$1@newscl01ah.mathworks.com>...
> Hi,
>
> You can use multivariable Newton-Raphson method.
> Just read this link
> http://faculty.washington.edu/finlayso/ebook/algebraic/NR/NRmult.htm
> and apply it to your case.
>
> Hopefully it will help.
>
> Good luck.

 It looks not easy task to mat-lab beginner , but I have to try .

thank you

Subject: Solving simultaneous non linear equations

From: Torsten

Date: 2 Jul, 2013 08:51:10

Message: 14 of 14

"mazen" wrote in message <kqs82f$qhf$1@newscl01ah.mathworks.com>...
> "Timothy Siahaan" <timmy_fisika_ugm_99@yahoo.co.id> wrote in message <kqk5c7$lf1$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > You can use multivariable Newton-Raphson method.
> > Just read this link
> > http://faculty.washington.edu/finlayso/ebook/algebraic/NR/NRmult.htm
> > and apply it to your case.
> >
> > Hopefully it will help.
> >
> > Good luck.
>
> It looks not easy task to mat-lab beginner , but I have to try .
>
> thank you

Try
res=@(x)(sum(a.*cos(x*h))-y)')
where a=(1,-1,1,-1,...)

Best wishes
Torsten.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us