How to iterate an equation to solve for missing variable

18 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Joseph Wilson
Joseph Wilson el 4 de Feb. de 2021
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 comentario
Terry Poole
Terry Poole el 4 de Feb. de 2021
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by