How to iterate an equation to solve for missing variable

조회 수: 25 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by