How to iterate an equation to solve for missing variable

29 ビュー (過去 30 日間)
Terry Poole
Terry Poole 2021 年 2 月 4 日
コメント済み: Terry Poole 2021 年 2 月 4 日
So I need to run a loop that inserts a value into a variable inside an equation until the equation equals a known amount.
Known amount: Thrust Coefficient (CT) = 0.0071
Equation: CT = ((1/2)*sigma*a)*(((1/3)*theta)-((1/2)*sqrt(CT/2)))
Non-working code:
close all; clear all; clc;
CT = 0.0071;
sigma = 0.0850;
a = 6;
theta = 0:360;
for i = theta
CT(i) = ((1/2)*sigma*a)*(((1/3)*theta)-((1/2)*sqrt(CT/2)));
if CT(i) == CT
break
end
end

採用された回答

Joseph Wilson
Joseph Wilson 2021 年 2 月 4 日
So there are a couple problems here:
1) the for loop can't start at zero so change to
for i = 1:length(theta) solves that
2) CT will try to override itself so need a new variable: change made after 3)
3) theta needs to be indexed in the equation to use only a single value so equation is then:
CT_test(i) = ((1/2)*sigma*a)*(((1/3)*theta(i))-((1/2)*sqrt(CT/2)));
4) you have CT in your equation... not sure if you need to do a little more algebra to solve that to the left side or not...
5) your step values of theta once it starts working are too large to find a solution
theta = 0:0.001:360
6) your if statement has no room for error so change to:
if abs(CT_test(i)-CT) < 0.00001
break
end
final solution:
close all; clear all; clc;
CT = 0.0071;
sigma = 0.0850;
a = 6;
theta = 0:0.001:360;
for i = 1:length(theta)
CT_test(i) = ((1/2)*sigma*a)*(((1/3)*theta(i))-((1/2)*sqrt(CT/2)));
if abs(CT_test(i)-CT) < 0.00001
break
end
end
  1 件のコメント
Terry Poole
Terry Poole 2021 年 2 月 4 日
Thanks a bunch!
Worked perfectly.
I knew it was something small but I've been staring at this code for so many hours now that my brain just isn't functioning properly.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by