solving a multiple variable equation with variables that increment

3 views (last 30 days)
So my program is an attempt to create a simple wind tunnel simulator to test different projectiles. here is the script:
function answer = efficiency()
syms V C A
%% Drag force equation = (1/2)pV^2CA
%p = density of air = 0.00128 g/mL
%V = velocity = variable V
%C = Coefficient of drag = variable C
%A = Cross sectional area = function A
%F = 0.5 * 0.00128(g/mL) * V^2(m/s^2) * C * A(m)
%%Drag Coefficient
% Drag Coefficient of Sphere = 0.47
% Drag Coefficient of cone = 0.50
% Drag Coefficient of cube = 1.05
% Drag Coefficient of long cylinder = 0.82
% Selection of projectile shape
X = menu('projectile shape','sphere','cone','cube','cylinder','main menu');
if X == 1
C = 0.47;
elseif X == 2
C = 0.50;
elseif X == 3
C = 1.05;
elseif X == 4
C = 0.82;
elseif X ==5
C = WindTunnel();
end
%%Cross sectional area
% area of sphere = pi r^2
% area of cone = pi r^2
% area of cube = base * height
% area of long cylinder = pi r^2
for A = .1:.1:1
end
for V = 100:100:1000
end
answer = solve(.00064*C*A*V^2);
answer =
end
It is only outputing zeros for the solutions any help would be appreciated

Accepted Answer

Star Strider
Star Strider on 11 Jun 2014
I haven’t run your code, but two problems are immediately apparent:
First, these two lines will simply loop without doing anything, and return the last value of A and V respectively:
for A = .1:.1:1
end
for V = 100:100:1000
end
Second in this line:
answer = solve(.00064*C*A*V^2);
what are you solving for?
If you want ‘answer’ to be the result of that calculation, simply do:
answer = .00064*C*A*V^2;
If you want it for every value of A and V, you have to put it into nested loops and index it.
For example:
A = .1:.1:1;
V = 100:100:1000;
for k1 = 1:length(A)
for k2 = 1:length(V)
answer(k1,k2) = 0.00064*C*A(k1)*V(k2)^2;
end
end
Make necessary changes so it does what you want.
A third observation is that you don’t need to call the Symbolic Math Toolbox at all. It is not well-suited to iterative calculations, and you’re not doing anything with it.
The last line in your code is simply ‘answer =’. I assume that’s a typo in typing your Question, because it would throw an error at compilation and your code wouldn’t calculate anything.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!