Solve equations in a loop with fsolve

Hi there,
I have two problems solving an equation:
Problem 1:
The equation below is to be solved component by component and the results are to be stored line by line in the vector F1. So far so good, how do I teach the loop to use the correct column for the calculations (e.g. f1 (f ,:) or f8 (f ,:) without integrating the function into fsolve?
tau = 0.1
f4 = [3; 2; 6; 8]
f8 = [2; 6; 7; 3]
eq = @(s) s*tau-(0.1.*s^2+3.54.*s-9.53).*f4.^2-f8;
for f = 1:1:length (f4)
F1 (f,:) = fsolve (eq, 0)
end
Problem 2:
The eq described above actually consists of two equations:
eq1 = 0.01*s.^2+3.54.*s-y*9.53
eq2 = y.*f4.^2-f8-s.*tau
It would be desirable to be able to insert both equations separately. Here is the variable y, which disappears after summarizing. Is there a way to combine this with the "problem" above?
Thanks a lot!

2 件のコメント

darova
darova 2020 年 2 月 18 日
Shouldn't the function be dependent?
Mepe
Mepe 2020 年 2 月 18 日
s is the variable we are looking for. f4 and f8 are given by the vectors. Do these still have to be specified as you have declared?

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

 採用された回答

Matt J
Matt J 2020 年 2 月 18 日
編集済み: Matt J 2020 年 2 月 18 日

0 投票

Your equations are quadratic and therefore generally have two solutions, s. Fsolve cannot find them both for you. Why aren't you using roots()? Regardless, here are the code changes pertaining to your question:
Problem 1
tau = 0.1
f4 = [3; 2; 6; 8]
f8 = [2; 6; 7; 3]
for i = 1:1:length (f4)
eq = @(s) s*tau-(0.1.*s^2+3.54.*s-9.53).*f4(i).^2-f8(i);
F1 (i,:) = fsolve (eq, 0);
end
Problem 2
for i = 1:1:length (f4)
eq1 = @(sy) [0.01*sy(1).^2+3.54.*sy(1)-sy(2)*9.53 ; ...
sy(2).*f4(i).^2-f8(i)-sy(1).*tau];
F2 (i,:) = fsolve (eq, [0,0]);
end

4 件のコメント

Mepe
Mepe 2020 年 2 月 18 日
Many many thanks! that has taken me a lot further. I will rethink the alternative use of roots ().
I have two more questions about your solution.
tau = 0.1
f4 = [3; 2; 6; 8]
f8 = [2; 6; 7; 3]
for i = 1:1:length (f4)
eq1 = @(sy) [0.01*sy(1).^2+3.54.*sy(1)-sy(2)*9.53 ; sy(2).*f4(i).^2-f8(i)-sy(1).*tau];
F2 (i,:) = fsolve (eq1, [0,0]);
end
What exactly do sy(1) or sy(2) mean in the equation definition?
in F2 the solution is filled with a 2x4 matrix. How does that come about?
Matt J
Matt J 2020 年 2 月 18 日
編集済み: Matt J 2020 年 2 月 18 日
What exactly do sy(1) or sy(2) mean in the equation definition?
In Problem 2, you have two unknowns, s and y. Fsolve requires that they be bundled into a vector sy=[s,y].
in F2 the solution is filled with a 2x4 matrix. How does that come about?
Each row is a solution [s,y] corresponding to i=1,2,3,4.
Mepe
Mepe 2020 年 2 月 19 日
Now I see. Thanks a lot for your help!!!
Mepe
Mepe 2020 年 2 月 19 日
I still have a question.
Now if I needed both solutions to the quadratic equation, how exactly would that work with the roots () command? according to help, a vector with numerical values must be used. How can I apply this to my problem?

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

その他の回答 (1 件)

darova
darova 2020 年 2 月 18 日

0 投票

This is the correct form
tau = 0.1
f4 = [3; 2; 6; 8]
f8 = [2; 6; 7; 3]
eq = @(s,f4,f8) s*tau-(0.1.*s^2+3.54.*s-9.53).*f4.^2-f8;
for f = 1:1:length (f4)
F1 (f,:) = fsolve (@(s)eq(s,f4(f),f8(f), 0);
end

3 件のコメント

Mepe
Mepe 2020 年 2 月 18 日
Thank you very much for your support. I really like your idea of how the fsolve expression is built!
Unfortunately I get an error message (The input to FSOLVE should be either a structure with valid fields or consist of at least two arguments).
Can you help again here?
tau = 0.1
f4 = [3; 2; 6; 8]
f8 = [2; 6; 7; 3]
eq = @(s,f4,f8) s*tau-(0.1.*s^2+3.54.*s-9.53).*f4.^2-f8;
for f = 1:1:length (f4)
F1 (f,:) = fsolve (@(s)eq(s,f4(f),f8(f), 0));
end
darova
darova 2020 年 2 月 18 日
I made a terrible mistake
Mepe
Mepe 2020 年 2 月 19 日
No problem. Thanks a lot for this solution!!!

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

カテゴリ

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

質問済み:

2020 年 2 月 18 日

コメント済み:

2020 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by