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!