"Size inputs must be scalar".

145 次查看(过去 30 天)
sadee
sadee 2014-6-2
Hi guys, can someone please explain me. how I can fix the error "size inputs must be scalar". I read some stuff to sort out this error, even after that I could not solve it. I want to plot loglog dt versus error. for this purpose I want that T(number of time grids) will be different for every iteration. and T could have 10,20,30 values even more. here is my code. its a one dim wave equation. Thanks in advance!
N=10;
X = 100;
dx = pi/X;
x(1,:)=0:dx:pi;
c=1;
tfinal=5;
T=10.^(3:5);
dt=tfinal./T;
for i=1:length(T)
t0=0;
t(:,1)= (0:T)'*dt(i);
uexac = zeros(T+1,X+1);
U = zeros(T+1,X+1);
beta=zeros(N,1);
alpha=zeros(N,1);
v0=0;
u0=sin(3*x);
U0=u0;
V0=v0;
U(1,:)=U0;
U(2,:)=dt(i)*V0+ U0;
%exact solution by fourier series
for n= 1:N
beta(n)=2/pi*trapz(x,v0.*cos(n*x));
alpha(n)=2/pi*trapz(x,u0.*sin(n*x));
an=alpha(n)*cos(n*c*t)+beta(n)/(n*c)*sin(n*c*t);
uexac=uexac+ an*sin(n*x);
end
%approximate solution by finite difference method
for j = 3:T+1
U1 = 2*U(j-1,:)-U(j-2,:);
U1=U1(2:end-1);
U2 = U(j-1,1:end-2)-2*U(j-1,2:end-1)+U(j-1,3:end);
U(j,2:end-1) = U1 + U2*c*(dt(i)^2/dx^2);
end
% compute error and store
error(i,1)=(max(max(abs(uexac-U))));
tt(i,1)=dt(i); %store time step size
end
Error using zeros
Size inputs must be scalar.
loglog(tt(:,1),error(:,1),'*');
xlabel('dt','fontSize',8);
ylabel('error','fontSize',8);
  2 个评论
Roger Wohlwend
Roger Wohlwend 2014-6-2
Your code does not run because several variables are missing: tfinal, X, ... So I cannot reproduce the error.
sadee
sadee 2014-6-2
Roger now I have put the rest variables. You can check

请先登录,再进行评论。

回答(3 个)

Henric Rydén
Henric Rydén 2014-6-2
编辑:Henric Rydén 2014-6-2
Here is one problem:
T = [1000 10000 100000]
uexac = zeros(T+1,X+1);
T is a vector. zeros(m,n) creates an m-by-n matrix so m and n need to be scalars, not vectors.
If you want three vectors, you need three commands. Like this
uexac1 = zeros(T(1)+1,X+1);
uexac2 = zeros(T(2)+1,X+1);
uexac3 = zeros(T(3)+1,X+1);
  8 个评论
sadee
sadee 2014-6-2
I have defined N, but it was showing in end of text. Now tried as you said but it's not working
Roger Wohlwend
Roger Wohlwend 2014-6-2
Then show us the updated code and what error occurs.

请先登录,再进行评论。


Suman Ahmed
Suman Ahmed 2020-11-22
编辑:Walter Roberson 2023-6-10
would you please help me to resolve the one error that is size input must be scalar?
A=[-0.0159 0 0.0418 0; 0 -0.011 0 0.0333; 0 0 -0.0419 0; 0 0 0 -0.033];
B=[-0.083325 0; 0 0.0628; 0 0.0479; 0.0312 0];
C=[0.50 0 0 0; 0 0.50 0 0; 0 0 0.50 0; 0 0 0 0];
D=zeros(4,2);
t=100;
t=1:1:t;
Q1=0.00005*ones(1,t);
Q2=0.000004*ones(1,t);
u=[Q1, Q2];
sys=ss(A,B,C,D);
sys_dis=c2d(sys,1);
Ad=sys_dis.a;
Bd=sys_dis.b;
Cd=sys_dis.c;
Dd=sys_dis.d;
Xint=[0; 0 ;0];
X(:,1)=Xint;
Yint=[0; 0 ;0];
y(:,1)=Yint;
for i=1:t-1
[ y(:,i+1), X(:,i+1) ] = out_cal(Ad,Bd,Cd,Dd,(u(i,:))',X(:,i) );
end
% defining output for plotting
h1=y(1,:);
h2=y(2,:);
h3=y(3,:);
% Plotting inputs
figure(1);
subplot(2,1,1);
plot(t,Q1,'r-');
title('Time VS Q_1');
ylabel('Q_1');
xlabel('time(s)');
subplot(2,1,2);
plot(t,Q2,'r-');
title('Time VS Q_2');
ylabel('Q_2');
xlabel('time(s)');
% Plotting outputs
figure(2);
subplot(4,1,1);
plot(t,h1,'r-');
title('Time VS level in the tank1');
ylabel('h1_t');
xlabel('time(s)');
subplot(4,1,2);
plot(t,h2,'r-');
title('Time VS level in the tank2');
ylabel('h2_t');
xlabel('time(s)');
subplot(4,1,3);
plot(t,h3,'r-');
title('Time VS level in the tank3');
ylabel('h3_t');
xlabel('time(s)');
%grid
  2 个评论
Steven Lord
Steven Lord 2020-11-23
t=100;
t=1:1:t;
Q1=0.00005*ones(1,t);
Q2=0.000004*ones(1,t);
t starts off as the value 100, but then on the next line becomes the vector [1, 2, 3, 4, ... 98, 99, 100]. You then try to use that to build a vector.
Since I know you don't want to try to build a 1-by-1-by-2-by-3-by-4-by-...-by-98-by-99-by-100 array (storing each element of the array in a different atom in this universe, you would still fall well short of the number of atoms needed) I have to assume you want to create 100 separate vectors. If so, use a for loop to process each element of t in turn or create a cell array with 100 elements where element k of that cell array corresponds to element k of the vector t.
If you want to create one vector with 100 elements, delete the line of code where you redefine t from being 100 to being the vector.
Suman Ahmed
Suman Ahmed 2020-11-23
thanks steven, it works now.

请先登录,再进行评论。


Elis
Elis 2023-6-10
% Introduce your personal data here
Dateofbirth = 960920; % yymmdd Use your birth date
% Compute the parameters
d1 = mod(Dateofbirth, 123);
c = d1 + 25;
a = 36 * c + 2 * mod(d1, 20);
b = 24 * c + mod(d1, 20);
% Given Data: do not change the order of links and nodes given in these matrices!
Redarc = [1 2 % Red links (first element of row k is the starting node of the k-th arc, second element the final node)
1 3
2 4
2 5
3 5
3 6
4 7
5 7
5 8
6 8
7 9
8 9];
Bluearc = [2 1 % Blue links (first element of row k is the starting node of the k-th arc, second element the final node)
1 3
4 2
2 5
5 3
3 6
4 7
7 5
5 8
8 6
7 9
9 8];
% Compute node-arc incidence matrix as explained in pages 75-76 of ASKS. Don't remove the last row. Linprog will take care of linear dependency.
numNodes = max(max(Redarc), max(Bluearc)); % Total number of nodes in the networks
numRedArcs = size(Redarc, 1);
numBlueArcs = size(Bluearc, 1);
Ared = zeros(numNodes, numRedArcs);
Error using zeros
Size inputs must be scalar.
Ablue = zeros(numNodes, numBlueArcs);
for i = 1:numRedArcs
startNode = Redarc(i, 1);
endNode = Redarc(i, 2);
Ared(startNode, i) = -1;
Ared(endNode, i) = 1;
end
for i = 1:numBlueArcs
startNode = Bluearc(i, 1);
endNode = Bluearc(i, 2);
Ablue(startNode, i) = -1;
Ablue(endNode, i) = 1;
end
%% Part (a): Minimize link load
% Compute the dimensions of Aeq and beq
numArcs = numRedArcs + numBlueArcs;
numConstraints = numArcs + 1; % Additional constraint for linear dependency
% Define the objective function
c = ones(numArcs, 1);
% Define the equality constraints
Aeq = [Ared; Ablue];
beq = [a; b];
% Add the row for linear dependency
Aeq(numConstraints, :) = 0;
beq(numConstraints) = 0;
% Define the inequality constraints (none in this case)
Ain = [];
bin = [];
% Define the upper and lower bounds (non-negative flow)
ub = Inf(numArcs, 1);
lb = zeros(numArcs, 1);
% Solve the linear programming problem
options = optimoptions('linprog', 'Display', 'off'); % Suppress the output display
Sol = linprog(c, Ain, bin, Aeq, beq, lb, ub, options);
% Extract the optimal flow for red and blue arcs
RedFlow = Sol(1:numRedArcs);
BlueFlow = Sol(numRedArcs + 1:end-1);
% Calculate the link loads (total flow on each link)
LinkLoads = RedFlow + BlueFlow;
% Calculate the maximal load among all links
w = max(LinkLoads);
% Calculate the node loads (total flow on each node)
NodeLoads = Ared(:, 1:numRedArcs) * RedFlow + Ablue(:, 1:numBlueArcs) * BlueFlow;
% Display the results
fprintf('Part (a): Minimize link load\n');
fprintf('Optimal Red Flow: %s\n', mat2str(RedFlow));
fprintf('Optimal Blue Flow: %s\n', mat2str(BlueFlow));
fprintf('Link Loads: %s\n', mat2str(LinkLoads));
fprintf('Maximal Load: %.2f\n', w);
fprintf('Node Loads: %s\n', mat2str(NodeLoads));
%% Part (b): Minimize node load
% Define the objective function
c2 = ones(numNodes, 1);
% Define the equality constraints
Aeq2 = [Ared; Ablue];
beq2 = [a; b];
% Define the inequality constraints (none in this case)
Ain2 = [];
bin2 = [];
% Define the upper and lower bounds (non-negative flow)
ub2 = Inf(numNodes, 1);
lb2 = zeros(numNodes, 1);
% Solve the linear programming problem
Sol2 = linprog(c2, Ain2, bin2, Aeq2, beq2, lb2, ub2, options);
% Extract the optimal flow for red and blue arcs
RedFlow2 = Ared * Sol2;
BlueFlow2 = Ablue * Sol2;
% Calculate the link loads (total flow on each link)
LinkLoads2 = RedFlow2 + BlueFlow2;
% Calculate the maximal load among all links
w2 = max(LinkLoads2);
% Calculate the node loads (total flow on each node)
NodeLoads2 = Sol2;
% Display the results
fprintf('\nPart (b): Minimize node load\n');
fprintf('Optimal Red Flow: %s\n', mat2str(RedFlow2));
fprintf('Optimal Blue Flow: %s\n', mat2str(BlueFlow2));
fprintf('Link Loads: %s\n', mat2str(LinkLoads2));
fprintf('Maximal Load: %.2f\n', w2);
fprintf('Node Loads: %s\n', mat2str(NodeLoads2));
  1 个评论
Walter Roberson
Walter Roberson 2023-6-10
RedArc and BlueArc are something-by-2.
numNodes = max(max(Redarc), max(Bluearc)); % Total number of nodes in the networks
max of a something-by-2 will proceed along the columns, giving a 1 x 2 result. You then use max() with two parameters where each of the parameters is 1 x 2: the result will be element-by-element maximums between the two vectors, so you will get a 1 x 2 result. So numNodes will be 1 x 2.
When you use max(A,B) then that is not the same as max([A,B]) or max([A;B])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by