I have a for loop creating a super equation that is the sum of a group of equations that could use variables A1 to A7. Each looping would increase the amount of As, which represents the amount of equations in that group. The super equation would then be solved.
cb = 1/5.25; %reverse of aspect ratio
cla = 2*pi; %the slope of lift vs aoa
aa = 5*pi/180; % radiant value of 5 degree
N = [3 5 7]; %list of possible N value
syms a1 a2 a3 a4 a5 a6 a7 temp
A = [a1 a2 a3 a4 a5 a6 a7]; %list of possible An
eqn = A; %list of possible equations
solved = A;
for i = 1:3
counter = N(i);
Acou = A(1:counter);
anslist = Acou;
eqncou = eqn(1:counter);
spandiv = (pi/2)/counter;
for j = 1:counter
for k = 1:counter
if rem(j,2)~=0
anslist(k) = A(k)*(sin(k*(spandiv*j)))+0.25*cb*cla*sin(k*(spandiv*j))/sin(spandiv*j);
else
anslist(k) = 0;
end
temp = temp + anslist(k);
end
eqncou(j) = temp == 0.25*cb*2*pi*aa;
end
solve(eqncou)
end
However, instead of seeing a solution in a1 to a3 or a5 or a7, a3, a5, and a7 are always missing from the solutions and instead, there is a temp in the solution. Could you tell me why?
ans =
struct with fields:
a1: [1×1 sym]
a2: [1×1 sym]
temp: [1×1 sym]
ans =
struct with fields:
a1: [1×1 sym]
a2: [1×1 sym]
a3: [1×1 sym]
a4: [1×1 sym]
temp: [1×1 sym]
ans =
struct with fields:
a1: [1×1 sym]
a2: [1×1 sym]
a3: [1×1 sym]
a4: [1×1 sym]
a5: [1×1 sym]
a6: [1×1 sym]
temp: [1×1 sym]
I want the output to just solve whatever given variable (A1 to A3, A1 to A5, etc), and don't output the temp. Is it possible?
EDIT: I have changed the code to force the solver to spit out the variable in A. However, it seems like my plan to construct an expression using for loops, then change that expression into an equation and put it into the solver has failed. The solver interprets my "temp" as a variable, rather than a solution:
cb = 1/5.25; %reverse of aspect ratio
cla = 2*pi; %the slope of lift vs aoa
aa = 5*pi/180; % radiant value of 5 degree
N = [3 5 7]; %list of possible N value
syms a1 a2 a3 a4 a5 a6 a7 temp
A = [a1 a2 a3 a4 a5 a6 a7]; %list of possible An
eqn = A; %list of possible equations
solved = A;
for i = 1:3
counter = N(i);
Acou = A(1:counter);
anslist = Acou;
eqncou = eqn(1:counter);
spandiv = (pi/2)/counter;
solved = Acou;
for j = 1:counter
for k = 1:counter
if rem(j,2)~=0
anslist(k) = A(k)*(sin(k*(spandiv*j)))+0.25*cb*cla*sin(k*(spandiv*j))/sin(spandiv*j);
else
anslist(k) = 0;
end
temp = temp + anslist(k);
end
eqncou(j) = temp == 0.25*cb*2*pi*aa;
end
Asol = vpasolve(eqncou, Acou);
if counter == 3
Asol.a1
Asol.a2
Asol.a3
end
end
The result for the first reiteration looks like this:
ans =
0.00000000000000014141003182998759606675517428884*temp + 0.0000000000000001598784692421062131291124254426
ans =
- 1.1547005383792516106614175041608*temp - 1.6047060746495452365715355746874
ans =
0
How to make 'temp' not a variable?
This should work. I don't if as expected, since your question is not entirely clear yet. Notice that all constants (as cb
, cla
) were declared as a symbolic object, to avoid fraction approximations of real numbers. The temp
variable is initially declared as (symbolic) zero, to create the equation using the for loops.
Clearly, you should assign a variable to the solve command, in order to access the solution to the equations. You can also try to simplify the solution, you find it suitable.
cb = sym('1')/5.25; %reverse of aspect ratio
cla = 2*sym('pi'); %the slope of lift vs aoa
aa = 5*sym('pi')/180; % radiant value of 5 degree
N = [3 5 7]; %list of possible N value
syms a1 a2 a3 a4 a5 a6 a7
A = [a1 a2 a3 a4 a5 a6 a7]; %list of possible An
eqn = A; %list of possible equations
solved = A;
for i = 1:3
counter = N(i)
Acou = A(1:counter);
anslist = Acou;
eqncou = eqn(1:counter);
spandiv = (sym('pi')/2)/counter;
temp = sym('0');
for j = 1:counter
for k = 1:counter
if rem(j,2)~=0
anslist(k) = A(k)*(sin(k*(spandiv*j)))+0.25*cb*cla*sin(k*(spandiv*j))/sin(spandiv*j);
else
anslist(k) = 0;
end
temp = temp + anslist(k);
end
eqncou(j) = temp == 0.25*cb*2*pi*aa;
end
solve(eqncou,Acou)
end