HELP!!! Subscripted assignment dimension mismatch.

1 次查看(过去 30 天)
I got this from my professor and I revised it to do some calculation, but it cracks when I change the parameter "thetas(1,1)= -50" into "thetas(1,1)= -50:1:20". I'm using this code to plot the thermal efficiency (eta) versus start degree of energy release (θs) from -50° to 20°. When I ran the code it popped out "Subscripted assignment dimension mismatch."
PLEASE HELP ME!!!!!!!!!!!
function []=FiniteHeatRelease_eta_test()
% Gas cycle heat release code for three engines
% engine parameters
thetas= -50:1:20; % Engine1 start of heat release (deg)
thetad= 20; % Engine1 duration of heat release (deg)
r=10; %compression ratio
gamma= 1.4; %gas const
q= 20; % dimensionless total heat release Qin/P1V1
a= 5; %weibe parameter a
n= 3; %weibe exponent n
step=1; % crankangle interval for calculation/plot
NN=360/step; % number of data points
% initialize the results data structure
save.theta=zeros(NN,1); % crankangle
save.vol=zeros(NN,1); % volume
save.press=zeros(NN,2); % pressure
save.work=zeros(NN,2); % work
fy(1) = 1;
fy(2) = 0;
% for loop for engine1
theta = -180; %initial crankangle
thetae = theta + step; %final crankangle in step
% for loop for pressure and work calculation
for i=1:NN,
[fy, vol] = integrate(theta,thetae,fy);
% reset to next interval
theta = thetae;
thetae = theta+step;
% copy results to output vectors
save.theta(i)=theta;
save.vol(i)=vol;
save.press(i)=fy(1);
save.work(i)=fy(2);
end %end of pressure and work iteration loop
w1=save.work(NN,1);
eta1= w1/q; % thermal efficiency
%plot results
plot(save.theta,eta1,'-', 'linewidth',2)
set(gca, 'fontsize', 18,'linewidth',2);
%grid
xlabel('start degree of energy release θ_s (°)','fontsize', 18)
ylabel('Thermal efficiency','fontsize', 18)
function[fy,vol] = integrate(theta,thetae,fy)
% ode23 integration of the pressure differential equation
% from theta to thetae with current values of fy as initial conditions
[tt, yy] = ode23(@rates, [theta thetae], fy);
%put last element of yy into fy vector
for k=1:2
fy(k) = yy(length(tt),k);
end
%pressure differential equation
function [yprime] = rates(theta,fy)
vol=(1.+ (r -1)/2.*(1-cosd(theta)))/r; % Eq 2.38
dvol=(r - 1)/2.*sind(theta)/r*pi/180.; %dvol/dtheta (Eq 2.39)
dx=0.; %set heat release to zero
if(theta > thetas) % then heat release dx > 0
dum1=(theta -thetas)/thetad;
x=1.- exp(-(a*dum1^n)); % Eq 2.26
dx=(1-x)*a*n*dum1^(n-1)/thetad; %dx/dthetha % Eq 2.27
end
term1= -gamma*fy(1)*dvol/vol;
term2= (gamma-1)*q*dx/vol;
yprime(1,1)= term1 + term2; % Eq 2.36
yprime(2,1)= fy(1)*dvol; % Eq 2.37; fy(1) is pressure
end %end of function rates
end %end of function integrate2
end % heat_release_weibe2
THANKS!!!!!!

回答(1 个)

Matt Cohen
Matt Cohen 2016-5-16
Greetings.
I understand that you are encountering issues, such as a subscripted dimension mismatch error, when running the provided function. These issues are likely being produced due to the modifications you are trying to make to the 'thetas' variable.
In the "FiniteHeatRelease_eta_test" code, it appears that the variable 'thetas' is expected to be a scalar, not an array. Some of the calculations and conditions need to be updated to handle arrays. As an example, the "rates" sub-function needs to be updated to handle element-wise operations.
For example, consider the following lines of code:
dum1=(theta -thetas)/thetad;
x=1.- exp(-(a*dum1^n)); % Eq 2.26
dx=(1-x)*a*n*dum1^(n-1)/thetad; %dx/dthetha % Eq 2.27
If 'thetas' is an array, then 'dum1' will also be an array since it will involve array subtraction. The next two lines of code, which compute 'x' and 'dx', would throw errors because they do not properly handle arrays. They need to be updated to perform element-wise operations on the data. The following code should take this into account:
dum1=(theta -thetas)/thetad;
x=1.- exp(-(a*dum1.^n)); % Eq 2.26
dx=(1-x)*a*n.*dum1.^(n-1)./thetad; %dx/dthetha % Eq 2.27
Notice the .^, .* and ./ operators, which perform element-wise power, element-wise multiplication, and element-wise division, respectively.
There seem to be some other issues that need to also be accounted for in order to make this function work. You might run into indexing issues since your code will now have more arrays present. For instance, you mentioned changing "thetas(1,1)= -50" into "thetas(1,1)= -50:1:20"; the second expression will throw an error because you are trying to assign an array of length 71 to a single element of 'thetas'. There is a dimension mismatch here.
Try making the changes suggested above and see if this helps to eliminate most of the errors. Try to also consider the other things I suggested, as these should likely help you to track down any other errors. Additionally, if you find that you are having difficulty understanding handling arrays in MATLAB, the MATLAB On-Ramp training might help you to get better acquainted with the language.
I hope this information proves to be helpful.
Matt

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by