How to iterate an equation to solve for missing variable
18 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!