Solving a system of function handles with fsolve

I have a problem using fsolve in order to solve a system of equations.
a1 to a6 are anonymous functions which are supposed to be equal to 0. T is a vector with six unknown variables T1 to T6. Each anonymous function consists of a term with at least two of those variables.
So with 6 equations and 6 unknowns the solver should be able to solve the system of equations.
I am calling the function like this:
T0 = [0 0 0 0 0 0];
Temp = fsolve(CalcTemps,T0)
function fun = CalcTemps(T)
global a1 a2 a3 a4 a5 a6
fun(1) = a1;
fun(2) = a2;
fun(3) = a3;
fun(4) = a4;
fun(5) = a5;
fun(6) = a6;
end
In my understanding the code is not working as it is not possible to store function handles in fun. I also tried to create fun as a cell array so that the function handles can be stored but then I get an error as fun has to be a function handle and not a cell array.
So my question is: Is it possible to store multiple function handles (a1 to a6) in a single function handle (fun)?

 採用された回答

Stephan
Stephan 2018 年 10 月 24 日
編集済み: Stephan 2018 年 10 月 24 日

1 投票

Hi,
use:
calc_temp
function calc__tenp
T = @(T1,T2,T3,T4,T5,T6) [T1 T2 T3 T4 T5 T6]; % unknown variable vector
T_Amb = 20;
a1 = @(T)5*T(2)-T(1); % anonymous functions
a2 = @(T)2*T(3)-4*T(2);
a3 = @(T)T(4)-T(3);
a4 = @(T)2*T(5)-3*T(4);
a5 = @(T)3*T(6)-4*T(5);
a6 = @(T)5*T_Amb-4*T(6);
T0 = [20 20 20 20 20 20]; % initial guess
Temp = fsolve(@CalcTemps,T0)
function fun = CalcTemps(T)
fun(1) = a1(T); % ERROR is produced in this line
fun(2) = a2(T);
fun(3) = a3(T);
fun(4) = a4(T);
fun(5) = a5(T);
fun(6) = a6(T);
end
end
Results:
Temp =
31.2500 6.2500 12.5000 12.5000 18.7500 25.0000
Best regards
Stephan

2 件のコメント

Lennart Vogt
Lennart Vogt 2018 年 10 月 24 日
Yes it is working now. Thanks a lot!
Stephan
Stephan 2018 年 10 月 24 日
Please accept useful answers in order to help other people with similar Problems finding helpful Solutions in the future.

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

その他の回答 (1 件)

Matt J
Matt J 2018 年 10 月 23 日

1 投票

You need to evaluate your function at T,
function fun = CalcTemps(T)
global a1 a2 a3 a4 a5 a6
fun(1) = a1(T);
fun(2) = a2(T);
fun(3) = a3(T);
fun(4) = a4(T);
fun(5) = a5(T);
fun(6) = a6(T);
end

7 件のコメント

Lennart Vogt
Lennart Vogt 2018 年 10 月 23 日
編集済み: Lennart Vogt 2018 年 10 月 23 日
Alright I have done so, thank you so far. I am getting the error "Not enough input arguments." for the line
fun(1) = a1(T);
Any idea where that might come from?
Matt J
Matt J 2018 年 10 月 23 日
編集済み: Matt J 2018 年 10 月 23 日
You haven't shown us the definition of a1(), but apparently it expects more input arguments than just T.
Lennart Vogt
Lennart Vogt 2018 年 10 月 23 日
The anonymous function contains other variables and has the following structure:
k = 2;
v = [1 2 3];
a1 = @(T) v(k)*T;
Is there a way to do some kind of interim solution so that the anonymous function transforms from
a1 = @(T) v(k)*T;
to
a1 = @(T) 4*T;
?
I think that would make my code a lot easier to continue.
Matt J
Matt J 2018 年 10 月 23 日
編集済み: Matt J 2018 年 10 月 23 日
Ah, sorry. The problem has nothing to do with a1. You have an error in your call to fsolve.
Temp = fsolve(@CalcTemps,T0); % was missing the '@'
Lennart Vogt
Lennart Vogt 2018 年 10 月 23 日
Yes that was definetly missing. Still I am getting an error "Index exceeds array bounds.".
My functions are full of other variables as I mentioned before. I think i am getting an error with those variables.
Do you know a way to compute them as I showed in my previous comment?
Matt J
Matt J 2018 年 10 月 23 日
編集済み: Matt J 2018 年 10 月 23 日
You could define a1 like so
c=v(k);
a1=@(T) c*T;
but I don't think it will make a difference.
Lennart Vogt
Lennart Vogt 2018 年 10 月 24 日
So basically I can reduce my code to the folowing structure:
T = @(T1,T2,T3,T4,T5,T6) [T1 T2 T3 T4 T5 T6]; % unknown variable vector
T_Amb = 20;
a1 = @(T)5*T(2)-T(1); % anonymous functions
a2 = @(T)2*T(3)-4*T(2);
a3 = @(T)T(4)-T(3);
a4 = @(T)2*T(5)-3*T(4);
a5 = @(T)3*T(6)-4*T(5);
a6 = @(T)5*T_Amb-4*T(6);
T0 = [20 20 20 20 20 20]; % initial guess
Temp = fsolve(@CalcTemps,T0)
function fun = CalcTemps(T)
global a1 a2 a3 a4 a5 a6
fun(1) = a1(T); % ERROR is produced in this line
fun(2) = a2(T);
fun(3) = a3(T);
fun(4) = a4(T);
fun(5) = a5(T);
fun(6) = a6(T);
end
Still I am getting an error "Index exceeds array bounds." inside of the function.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2018a

質問済み:

2018 年 10 月 23 日

コメント済み:

2018 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by