error using the matrices to plot a graph
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone!
Could you please help me!
Error using .
To assign to or create a variable in a table, the number of rows must match the height of the table.
Error in tabular/addvars (line 184)
b = move(b).dotAssign(newvarnames{ii},varargin{ii}); % b.(newvarnames{ii}) = varargin{ii}
Error in untitled3 (line 85)
Table = addvars(Table, Vstall, 'After','V_p_min');
My code is:
clear all; close all
W = 10000;
S = 40;
AR=7;
cd0 = 0.005;
k = 1/pi/AR;
Psl=25000;
clalpha = 2*pi;
cdmin=4*cd0;
clmin=sqrt(3*cd0/k);
figure(1);hold on; xlabel('V');ylabel('P');
fun=@(n1) n1*W*(cdmin/clmin)*sqrt(2*n1*W/(1.225*S*clmin))-25000;
nmax = fzero(fun,2);
nmin=1/cos(0);
nv=nmin:0.1:2;
g=9.81;
%3.1
for k1 = 1:numel(nv)
n = nv(k1);
i=0;
for alpha = 1:0.25:12
i=i+1;
cl(i) = clalpha * alpha * pi/180;
V(i) = sqrt(2*n*W/1.225/S/cl(i));
cd(i) = cd0 + k * cl(i) * cl(i);
D(i) = n*W*cd(i)/cl(i);
P(i) = D(i)*V(i);
P_a(i)=Psl;
R(i)=V(i)*V(i)/(g*sqrt(n*n-1));
T(i)=2*pi*R(i)/V(i);
L(i)=0.5*1.225*V(i)*V(i)*S*cl(i);
n3(i)=L(i)/W;
end
c(k1,:) = V;
Pm(k1,:) = P;
Pma(k1,:) = P_a;
Rv(k1,:)=R;
Tv(k1,:)=T;
nk(k1,:)=n3;
xix = find(diff(sign(D.*V-P_a)));
if ~isempty(xix)
for k3 = 1:numel(xix)
idxrng = max(xix(k3)-1,1) : min(numel(V),xix(k3)+1);
xv(k3) = interp1(P(idxrng)-P_a(idxrng), V(idxrng), 0);
yv(k3) = interp1(V(idxrng),P(idxrng), xv(k3));
Xm{k1,k3} = xv(k3); % Intersection X-Matrix
Ym{k1,k3} = yv(k3); % Intersection Y-Matrix
end
end
figure(1); plot(V,P)
hold on
plot(V,P_a)
plot(xv, yv, 's', 'DisplayName',sprintf('Intersections %d',k1)) % Plot Intersections (Optional)
[minV,Vnidx] = min(V);
Vstall(k1,:) = minV;
Pstall(k1,:) = P(Vnidx);
nvv(:,k1)=nv;
[Pmin,idx] = min(P);
minP(k1,:) = Pmin;
Vm(k1,:) = V(idx);
N=nvv(:,1);
Vminp=Vm(:,1);
end
hold off
grid
figure(2)
plot(V, R, 'LineWidth',2, 'DisplayName','R(V)')
hold on
plot(c,Rv, 'DisplayName','Rv(c)')
hold off
grid
xlabel('V')
ylabel('R')
figure(3)
plot(T, R, 'LineWidth',2, 'DisplayName','R(V)')
hold on
plot(Tv,Rv, 'DisplayName','Rv(c)')
hold off
grid
xlabel('V')
ylabel('R')
% legend('Location','eastoutside')
Table = cell2table(cat(2,Xm,Ym), 'VariableNames',{'V_p_max','V_p_min','Pmax','Pmin'});
Table = addvars(Table, Vstall, 'After','V_p_min');
Table = addvars(Table, N, 'Before','V_p_max');
Table = addvars(Table, Pstall, minP,Vminp, 'After','Pmin');
Vpmin=[Vstall(1:8);cell2mat(Xm(9,2));cell2mat(Xm(10,2))];
Vpmax=cell2mat(Xm(:,1));
figure(4)
plot(Vpmax,N)
hold on
plot(Vpmin,N)
hold off
grid
xlabel('V')
ylabel('n')
0 Kommentare
Antworten (2)
Kevin Holly
am 17 Feb. 2023
The length of some of your columns didn't match up. Original table had 10 rows. Some of the new variables added to the table had 11 rows. I picked the first 10 values in the new variables and ran the code below:
W = 10000;
S = 40;
AR=7;
cd0 = 0.005;
k = 1/pi/AR;
Psl=25000;
clalpha = 2*pi;
cdmin=4*cd0;
clmin=sqrt(3*cd0/k);
figure(1);hold on; xlabel('V');ylabel('P');
fun=@(n1) n1*W*(cdmin/clmin)*sqrt(2*n1*W/(1.225*S*clmin))-25000;
nmax = fzero(fun,2);
nmin=1/cos(0);
nv=nmin:0.1:2;
g=9.81;
%3.1
for k1 = 1:numel(nv)
n = nv(k1);
i=0;
for alpha = 1:0.25:12
i=i+1;
cl(i) = clalpha * alpha * pi/180;
V(i) = sqrt(2*n*W/1.225/S/cl(i));
cd(i) = cd0 + k * cl(i) * cl(i);
D(i) = n*W*cd(i)/cl(i);
P(i) = D(i)*V(i);
P_a(i)=Psl;
R(i)=V(i)*V(i)/(g*sqrt(n*n-1));
T(i)=2*pi*R(i)/V(i);
L(i)=0.5*1.225*V(i)*V(i)*S*cl(i);
n3(i)=L(i)/W;
end
c(k1,:) = V;
Pm(k1,:) = P;
Pma(k1,:) = P_a;
Rv(k1,:)=R;
Tv(k1,:)=T;
nk(k1,:)=n3;
xix = find(diff(sign(D.*V-P_a)));
if ~isempty(xix)
for k3 = 1:numel(xix)
idxrng = max(xix(k3)-1,1) : min(numel(V),xix(k3)+1);
xv(k3) = interp1(P(idxrng)-P_a(idxrng), V(idxrng), 0);
yv(k3) = interp1(V(idxrng),P(idxrng), xv(k3));
Xm{k1,k3} = xv(k3); % Intersection X-Matrix
Ym{k1,k3} = yv(k3); % Intersection Y-Matrix
end
end
figure(1); plot(V,P)
hold on
plot(V,P_a)
plot(xv, yv, 's', 'DisplayName',sprintf('Intersections %d',k1)) % Plot Intersections (Optional)
[minV,Vnidx] = min(V);
Vstall(k1,:) = minV;
Pstall(k1,:) = P(Vnidx);
nvv(:,k1)=nv;
[Pmin,idx] = min(P);
minP(k1,:) = Pmin;
Vm(k1,:) = V(idx);
N=nvv(:,1);
Vminp=Vm(:,1);
end
hold off
grid
figure(2)
plot(V, R, 'LineWidth',2, 'DisplayName','R(V)')
hold on
plot(c,Rv, 'DisplayName','Rv(c)')
hold off
grid
xlabel('V')
ylabel('R')
figure(3)
plot(T, R, 'LineWidth',2, 'DisplayName','R(V)')
hold on
plot(Tv,Rv, 'DisplayName','Rv(c)')
hold off
grid
xlabel('V')
ylabel('R')
% legend('Location','eastoutside')
Table = cell2table(cat(2,Xm,Ym), 'VariableNames',{'V_p_max','V_p_min','Pmax','Pmin'})
size(Table)
size(Vstall)
Vstall
addvars(Table, Vstall(1:10), 'After','V_p_min');
addvars(Table, N(1:10), 'Before','V_p_max');
addvars(Table, Pstall(1:10), minP(1:10),Vminp(1:10), 'After','Pmin');
Table
Vpmin=[Vstall(1:8);cell2mat(Xm(9,2));cell2mat(Xm(10,2))];
Vpmax=cell2mat(Xm(:,1));
figure(4)
plot(Vpmax,N(1:10))
hold on
plot(Vpmin,N(1:10))
hold off
grid
xlabel('V')
ylabel('n')
0 Kommentare
Walter Roberson
am 17 Feb. 2023
Verschoben: Walter Roberson
am 17 Feb. 2023
You test if ~isempty(xix) and do not assign to Xm{k1,:} or Ym{k1,:} in that case. That can lead to empty cells if it happens in the middle of the run, or can lead to Xm and Ym being shorter if it happens at the end of a run. Either way, if it happens, when you cell2mat() the result is not going to be as large as the other variables.
clear all; close all
W = 10000;
S = 40;
AR=7;
cd0 = 0.005;
k = 1/pi/AR;
Psl=25000;
clalpha = 2*pi;
cdmin=4*cd0;
clmin=sqrt(3*cd0/k);
figure(1);hold on; xlabel('V');ylabel('P');
fun=@(n1) n1*W*(cdmin/clmin)*sqrt(2*n1*W/(1.225*S*clmin))-25000;
nmax = fzero(fun,2);
nmin=1/cos(0);
nv=nmin:0.1:2;
g=9.81;
%3.1
for k1 = 1:numel(nv)
n = nv(k1);
i=0;
for alpha = 1:0.25:12
i=i+1;
cl(i) = clalpha * alpha * pi/180;
V(i) = sqrt(2*n*W/1.225/S/cl(i));
cd(i) = cd0 + k * cl(i) * cl(i);
D(i) = n*W*cd(i)/cl(i);
P(i) = D(i)*V(i);
P_a(i)=Psl;
R(i)=V(i)*V(i)/(g*sqrt(n*n-1));
T(i)=2*pi*R(i)/V(i);
L(i)=0.5*1.225*V(i)*V(i)*S*cl(i);
n3(i)=L(i)/W;
end
c(k1,:) = V;
Pm(k1,:) = P;
Pma(k1,:) = P_a;
Rv(k1,:)=R;
Tv(k1,:)=T;
nk(k1,:)=n3;
xix = find(diff(sign(D.*V-P_a)));
if ~isempty(xix)
for k3 = 1:numel(xix)
idxrng = max(xix(k3)-1,1) : min(numel(V),xix(k3)+1);
xv(k3) = interp1(P(idxrng)-P_a(idxrng), V(idxrng), 0);
yv(k3) = interp1(V(idxrng),P(idxrng), xv(k3));
Xm{k1,k3} = xv(k3); % Intersection X-Matrix
Ym{k1,k3} = yv(k3); % Intersection Y-Matrix
end
else
fprintf('xix empty for k1 = %d\n', k1);
end
figure(1); plot(V,P)
hold on
plot(V,P_a)
plot(xv, yv, 's', 'DisplayName',sprintf('Intersections %d',k1)) % Plot Intersections (Optional)
[minV,Vnidx] = min(V);
Vstall(k1,:) = minV;
Pstall(k1,:) = P(Vnidx);
nvv(:,k1)=nv;
[Pmin,idx] = min(P);
minP(k1,:) = Pmin;
Vm(k1,:) = V(idx);
N=nvv(:,1);
Vminp=Vm(:,1);
end
hold off
grid
figure(2)
plot(V, R, 'LineWidth',2, 'DisplayName','R(V)')
hold on
plot(c,Rv, 'DisplayName','Rv(c)')
hold off
grid
xlabel('V')
ylabel('R')
figure(3)
plot(T, R, 'LineWidth',2, 'DisplayName','R(V)')
hold on
plot(Tv,Rv, 'DisplayName','Rv(c)')
hold off
grid
xlabel('V')
ylabel('R')
% legend('Location','eastoutside')
Table = cell2table(cat(2,Xm,Ym), 'VariableNames',{'V_p_max','V_p_min','Pmax','Pmin'});
whos Xm Ym Table Vstall N Pstall minP Vminp
Table = addvars(Table, Vstall, 'After','V_p_min');
Table = addvars(Table, N, 'Before','V_p_max');
Table = addvars(Table, Pstall, minP,Vminp, 'After','Pmin');
Vpmin=[Vstall(1:8);cell2mat(Xm(9,2));cell2mat(Xm(10,2))];
Vpmax=cell2mat(Xm(:,1));
figure(4)
plot(Vpmax,N)
hold on
plot(Vpmin,N)
hold off
grid
xlabel('V')
ylabel('n')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!