Declare a variable in a function for system of equation solving.

I was solving a system of equations for an iteration problem in an editor code and it worked fine, but when I copied the same code to a function an error appeared when executed. I think it can not be used the "syms" function to declare variables inside a function, but I do not know how to declare those three variables otherwise. The code I was running was the following:
syms Tw_h Tw_c q
eqns = [...
-(k_placa.*A./esp).*(Tw_c-Tw_h)==q,...
h_hot.*A.*(T_hot-Tw_h)==q,...
h_cold.*A.*(Tw_c - T_cold)==q...
];
S2 = solve(eqns);
Tw_h = vpa(S2.Tw_h,9);
Tw_c = vpa(S2.Tw_c,9);
q = vpa(S2.q,9);
The error I obtained:
Error using sym/cat>checkDimensions (line 70)
CAT arguments dimensions not consistent.
Error in sym/cat>catMany (line 35)
[resz, ranges] = checkDimensions(sz,dim);
Error in sym/cat (line 27)
ySym = catMany(dim, args);
Error in sym/horzcat (line 19)
ySym = cat(2,args{:});
Error in coef_h (line 86)
-(k_placa.*A./esp).*(Tw_c-Tw_h)==q,...
Error in calculs_math (line 94)
daily_B3_bruto = [daily_B3_bruto coef_h(daily_B3_bruto,B3_B4)];
I guess there's a way of declaring the three variables I am using inside a function, but I couldn't find it and hoped somebody knew how it vould be done.
Thanks in advance.
Edited: I've attached a table sample and a sample function as well.

9 件のコメント

Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日
The message show that error occured on this line
daily_B3_bruto = [daily_B3_bruto coef_h(daily_B3_bruto,B3_B4)];
Whereas the code you posted does not have this line. The error is about inconsistent dimensions. Such errors are hard to debug with partial data and without an input dataset.
Walter Roberson
Walter Roberson 2020 年 3 月 24 日
No the problem occurred on the construction of the equations in the code shown not the outer routine.
Walter Roberson
Walter Roberson 2020 年 3 月 24 日
k_plata or h_hot or h_cold are not scalars.
Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日
correct. I now noticed that it is the definition of coef_h function.
Roger Gomila
Roger Gomila 2020 年 3 月 24 日
k_placa, A, esp are constant variables from one table
h_hot , h_cold, are the values which are calculated inside the function. These values do not result in any problem when are in the editor code, but inside the function they do. It must be the three variable within the equation to be solved that fail.
Walter Roberson
Walter Roberson 2020 年 3 月 24 日
We need the code (and sample data files) to test with.
John D'Errico
John D'Errico 2020 年 3 月 24 日
編集済み: John D'Errico 2020 年 3 月 24 日
I conjecture that some of those variables - are actually vectors or arrays? I say that because you are using .* to multiply. This is also consistent with the error message, which talks about dimensions. Yes, you CAN do what you seem to be talking about inside a function. But we don't really know EXACTLY what you are doing, and the devil is in the details.
Walter Roberson
Walter Roberson 2020 年 3 月 24 日
When you stop because of the error, what are:
size(A)
size(esp)
size(h_cold)
size(h_hot)
size(k_placa)
size(q)
size(T_cold)
size(T_hot)
size(Tw_c)
size(Tw_h)
Roger Gomila
Roger Gomila 2020 年 3 月 25 日
I have attached a sample test table and function to the main post. Hope it helps now!

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2020 年 3 月 25 日

0 投票

most of the variables in that function are column vectors. you are passing in a multi-row timetable and when you extract variables from that you get vectors. But not all of the entries are vectors so you are not able to build an 8 by 3 array of equations.
if you do try to solve () the set of 8 rows and 3 columns then remember that solve is strictly simultaneous equatitions, values of the variables that solve all the equations. It will not produce one set of answers per row. you may need to loop or arrayfun

3 件のコメント

Roger Gomila
Roger Gomila 2020 年 3 月 25 日
Would it be possible if I did the systems of equation solver within a new function, where there would just be the code for the equation, and called with rowfun ?
Walter Roberson
Walter Roberson 2020 年 3 月 25 日
Potentially that could work, after having adjusted the equations so that there are the same number of rows for each.
Roger Gomila
Roger Gomila 2020 年 3 月 28 日
Sorry, I forgot to accept your answer before.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by