Subject: Some doubts about using * fsolve * From: Charles Date: 12 May, 2012 00:12:22 Message: 1 of 12 Hello there community, I'm new with MATLAB, but, in fact, I've been reading and practicing in some way to get familiar with this beautiful programming language.  The thing is: I'm trying to solve 2 non-linear equation with the fsolve function but some doubts came up. I've the file with the 2 expression that I want to solve, another one with a declaration of a big matrix and another one with data. Since the only way to declare the fsolve is somewhat like function f = myfun(x), I mean, we just can "pass" the x as the argument of the function and no other one, my first question in related to how can I make the the file, where I've my 2 expression, see the other one that have the data. For example: function f = TransitoEnergia( x )     % x(1)=V1; % x(4)=S1;     % x(2)=V2; % x(5)=Sc3;     % x(3)=V3;     f(1)=y11*x(1)*conj(x(1)) + y12*x(2)*conj(x(1)) + y13*x(3)*conj(x(1)) - conj(x(4));     f(2)=y21*x(1)*conj(x(3)) + y22*x(2)*conj(x(3)) + y23*x(3)*conj(x(3)) - conj(x(5));      end I need to get the yXX values from the other file that I have the data. I don't have a clue of how to do it. Can anyone help me? Best regards, Charles
 Subject: Some doubts about using * fsolve * From: Sargondjani Date: 12 May, 2012 05:11:22 Message: 2 of 12 you seriously underestimate what is possible with matlab, haha. Anyway, this is what to do: you can pass y as a function argument:  function f = TransitoEnergia( x,y ) ...where y is the matrix: y(1,1)=y11, y(2,1)=y21, et cetera ...and then you only have to define the function which you are going to solve for x: TE_solve=@(x)TransitoEnergia(x,y); of course you have to input the 'y' somewhere. now you solve: x=fsolve(TE_solve,x0) and that's it
 Subject: Some doubts about using * fsolve * From: Charles Date: 12 May, 2012 15:10:22 Message: 3 of 12 Hello there Sargondjani and thank you for your reply. Well, so far so good. Now, I know that I can do that, but, yet, something is missing. For example, in c++ if you have implemented a function, when you are about to use that function in the main function, you need to use the #include in the file that contains the main function. Here the case is somewhat similar. I mean, the file that I am refering to as Matrix_admitancias, is the file that contains the data that I need to use in the function TransitoEnergia (those y11, y12, etc) as I told you before. So, how can the file or the function himself, that I am implementing (TransitoEnergia) know where is the other file Matrix_admintancias that contains that data? Do you understand?  I want to separate the data in a different file because I'll need those in other occasions. Instead of passing the matrix inside the function, I could define those y11, y12, etc inside the function and it would be okay. Best regards, Charles
 Subject: Some doubts about using * fsolve * From: Bruno Luong Date: 12 May, 2012 15:30:40 Message: 4 of 12 "Charles" wrote in message ... > Hello there Sargondjani and thank you for your reply. > > Well, so far so good. Now, I know that I can do that, but, yet, something is missing. > > For example, in c++ if you have implemented a function, when you are about to use that function in the main function, you need to use the #include in the file that contains the main function. > > Here the case is somewhat similar. I mean, the file that I am refering to as Matrix_admitancias, is the file that contains the data that I need to use in the function TransitoEnergia (those y11, y12, etc) as I told you before. So, how can the file or the function himself, that I am implementing (TransitoEnergia) know where is the other file Matrix_admintancias that contains that data? > There is no INCLUDE in MATLAB. Fundamentally, Matlab is fundamentally different than C (first of all it's not compile language. If you want to use data inside a function, you have to pass it as input parameters. You could use a same script to create same data in different functions, but this is not recommended in practice. Bruno
 Subject: Some doubts about using * fsolve * From: Marc Date: 12 May, 2012 17:08:29 Message: 5 of 12 "Bruno Luong" wrote in message ... > "Charles" wrote in message ... > > Hello there Sargondjani and thank you for your reply. > > > > Well, so far so good. Now, I know that I can do that, but, yet, something is missing. > > > > For example, in c++ if you have implemented a function, when you are about to use that function in the main function, you need to use the #include in the file that contains the main function. > > > > Here the case is somewhat similar. I mean, the file that I am refering to as Matrix_admitancias, is the file that contains the data that I need to use in the function TransitoEnergia (those y11, y12, etc) as I told you before. So, how can the file or the function himself, that I am implementing (TransitoEnergia) know where is the other file Matrix_admintancias that contains that data? > > > > There is no INCLUDE in MATLAB. > > Fundamentally, Matlab is fundamentally different than C (first of all it's not compile language. If you want to use data inside a function, you have to pass it as input parameters. > > You could use a same script to create same data in different functions, but this is not recommended in practice. > > Bruno You keep mentioning 'data' but all I see are six yXX coefficients. Is there something extremely special about these, as I assume these are numbers? If you have these six variables in another m file and that m file is stored in a folder that is on Matlab's path, then calling that m file from another m file is easy. function y = your_variables y = [ y11; y12 ; y13; y21; y22; y;23] ; end Of course you need to define those yXXs as numbers or with some calcs. A simple call from any file y = your_variables; Will get you your six. Seems like a silly way to grab six coefficients though. Hope this is what you were asking.
 Subject: Some doubts about using * fsolve * From: Charles Date: 12 May, 2012 20:39:27 Message: 6 of 12 Well, when I am referring data, those are variables that will be used in 2 other m files. Imagine, I have a m file that contains all the variables that I'll use: Dados_de_entrada. For example the electrical machine parameters, the lengths of the lines all some initial calculations that I want to do. Then, another m file where will contain a matrix with all the admittance that I will use, and will need to change according to some conditions - Opening a line, cause a short-circuit, etc: Matriz_y. I did it in this way: % function [Ymm Ymn Ynm Ynn] = Matriz_y (Xd, Xcc, Xeq, xl1, xl2, xl3, Zc1, Zc2, Zc3) Ymm(1,1) = 1/(1i*(Xd+Xcc)); Ymm(1,2) = 0; Ymm(2,1) = 0; Ymm(2,2) = 1/(1i*Xeq); Ymn(1,1) = -1/(1i*(Xd+Xcc)); Ymn(1,2) = 0; ... etc. end % I don't know if it is right. For implementing the expressions to do the calculations, I created another file: Expressoes_transito_energia. function f = Expressoes_transito_energia( x ) f(1) = y11*x(1)*conj(x(1)) + y12*x(2)*conj(x(1)) + y13*x(3)*conj(x(1)) - conj(x(4)) ; f(2) = y21*x(1)*conj(x(3)) + y22*x(2)*conj(x(3)) + y23*x(3)*conj(x(3)) - conj(x(5)) ; end The variables y11, y12, etc, are the variables that are inside the m file that i said that contains the matrix and the x(5) and x(4) are two variables that are inside of the Dados_de_entrada. Finally, to see the result from the fsolve, I created another m file called Transito_energia. % function x = Transito_energia %Valores iniciais x0 = [1; 1]; options = optimset('Display','iter'); x = fsolve(@Expressoes_transito_energia, x0, options); end % Now, how the function f = Expressoes_transito_energia( x ) knows where the y11, y12, x(4), x(5), etc are? (the comparison to c++ is that we would need to #include the file Dados_de_entrada. How it works here in matlab? The same goes to the m files of the matrix, how does the function [Ymm Ymn Ynm Ynn] = Matriz_y (Xd, Xcc, Xeq, xl1, xl2, xl3, Zc1, Zc2, Zc3) knows where are those variables? Since I will call this functions on the main m file, maybe I should do the kind of #include in that main m file (se same way that it would be for the function f = Expressoes_transito_energia( x ). Because this is the first time using the fsolve (and could say matlab) the way that I implemented the function f = Expressoes_transito_energia( x ) is ok? Thank you for your patient.
 Subject: Some doubts about using * fsolve * From: dpb Date: 12 May, 2012 20:59:48 Message: 7 of 12 On 5/12/2012 3:39 PM, Charles wrote: > Well, when I am referring data, those are variables that will be used in > 2 other m files. ... > The variables y11, y12, etc, are the variables that are inside the m > file that i said that contains the matrix and the x(5) and x(4) are two > variables that are inside of the Dados_de_entrada. ... Well, no they aren't. They're local variables inside the function because Matlab (like most other "modern" programming languages) has the concept of local scope/lifetime within each function. Unless a variable is made GLOBAL or passed into the function (or declared internally and only used internally) it is not available to the function. So, your function "knows nuthink" about the various y's you defined in another function. See the section "Passing extra parameters" referenced in the fsolve function reference for suggestions/examples on how to get your additional parameters into the function. --
 Subject: Some doubts about using * fsolve * From: Bruno Luong Date: 13 May, 2012 06:25:12 Message: 8 of 12 "Charles" wrote in message ... > > Since I will call this functions on the main m file, maybe I should do the kind of #include in that main m file (se same way that it would be for the function f = Expressoes_transito_energia( x ). No pass your yxx as PARAMETERS. Forget about #INCLUDE Bruno
 Subject: Some doubts about using * fsolve * From: Charles Date: 13 May, 2012 12:02:07 Message: 9 of 12 Hello again. Well, I did! Using the anonymous functions. The function: function f = Expressoes_transito_energia(x, Ynn, Sc3, s1, V2)          %v1=x(1); v2=x(2); v3=x(3);     f(1)=Ynn(1,1)*x(1)*conj(x(1)) + Ynn(1,2)*V2*conj(x(1)) + Ynn(1,3)*x(2)*conj(x(1)) - conj(s1);     f(2)=Ynn(3,1)*x(1)*conj(x(2)) + Ynn(3,2)*V2*conj(x(2)) + Ynn(3,3)*x(2)*conj(x(2)) - conj(Sc3);      end Calling the funtion: Dados_de_entrada; %% Uso do método fsolve para resolver o sistema de equações não lineares para obter o trânsito de energia. %Cálculo do V1 e V3 através do método fsolve [Ymm Ymn Ynm Ynn] = Matriz_y(Xd, Xcc, Xeq, xl1, xl2, xl3); %Valores iniciais para o fsolve guess=[1 1]; fun=@(x)Expressoes_transito_energia(x,Ynn, Sc3, s1, V2); [result, fval]=fsolve(fun, guess); %Resultado obtido com o fsolve V1 = result(1); V3 = result(2); I manage to pass the Ynn as an argument of the function using the anonymous function. Did it right? What you guys think about it?
 Subject: Some doubts about using * fsolve * From: Sargondjani Date: 13 May, 2012 21:54:08 Message: 10 of 12 at first inspection it looks like proper programming... except that i dont see why you want to pass Sc3 and s1 as inputs into the function, because you dont use those inputs inside the function
 Subject: Some doubts about using * fsolve * From: Charles Date: 13 May, 2012 22:39:10 Message: 11 of 12 "Sargondjani" wrote in message ... > at first inspection it looks like proper programming... except that i dont see why you want to pass Sc3 and s1 as inputs into the function, because you dont use those inputs inside the function Hello sagondjani, You can see those two variables in both expressions:  f(1)=Ynn(1,1)*x(1)*conj(x(1)) + Ynn(1,2)*V2*conj(x(1)) + Ynn(1,3)*x(2)*conj(x(1)) - > conj(s1) < ;     f(2)=Ynn(3,1)*x(1)*conj(x(2)) + Ynn(3,2)*V2*conj(x(2)) + Ynn(3,3)*x(2)*conj(x(2)) - > conj(Sc3) < ; Best regards, Charles.
 Subject: Some doubts about using * fsolve * From: Sargondjani Date: 14 May, 2012 11:05:08 Message: 12 of 12 haha. i read the lines 10 times and just didnt see them... anyway, does your program do what it should do??? i mean, thats the real test, no?

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### 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.