Solve a system of equations iteratively

3 次查看(过去 30 天)
I would like Matlab to solve a system of equations iteratively, so that two among the three solutions of the (i-1)-th iteration are used as input data for the i-th iteration.
This is the code I'm trying to run,
clear vars close all clc
global Beta Tchnm Deltat Alpha Teta Deltaz mchn Tfm Gamma mf Delta Epsi Ta
Alpha = 1.61659e-07; Beta = 0.01731788; Gamma = 1.008413695; Delta = 1.213117234; Epsi = 0.001017152; Teta = 0.461538462; Deltaz = 0.001; Deltat = 0.005; mf = 0.03; mchn = 0.03; T0 = 89.3 + 273.15; Ta = 298.15;
solveoptions = optimoptions('fsolve','Display','iter',... 'Algorithm','levenberg-marquardt');
X0 = [350 350 350]; Sol = zeros(10,3);
for i = 1:10 if i == 1 Tchnm(i) = T0; Tfm(i) = T0; Sol(i,:) = fsolve(@(x) Equations(x),X0,solveoptions) else Tchnm(i) = Sol(i-1,2); Tfm(i) = Sol(i-1,3); Sol(i,:) = fsolve(@(x) Equations(x),X0,solveoptions) end end
function f = Equations(x)
global Beta Tchnm Deltat Alpha Teta Deltaz mchn Tfm Gamma mf Delta Epsi Ta
Tchnp = x(1); Tchn = x(2); Tf = x(3);
Tfmean = (Tf + Tfm)/2; Tchnmean = (Tchn + Tchnm)/2;
f(1) = -(Tchn - Tchnm)/Deltat + (Alpha*(Tchnp - 2*Tchn + Tchnm)/Deltaz^2) + Beta*(1 + mchn*Tchn); f(2) = -(Tf - Tfm)/Deltaz + Gamma*(1 + mf*Tfmean) - Delta*(Tfmean - Tchnmean) - Epsi*(Tfmean - Ta); f(3) = (Tchn - Tchnm) + Teta*(Tchn - Tf);
f = f'; end
This is the message I get: Subscripted assignment dimension mismatch. I think that the error is in the for loop.
Thanks in advance, Riccardo Rinaldi

回答(1 个)

Arun Mathamkode
Arun Mathamkode 2018-4-20

If the error says 'Subscripted assignment dimension mismatch', that means you attempt to assign elements to an existing array, but the size of the variable you are trying to assign is not compatible with the existing array. Please refer the following answer for more details. https://www.mathworks.com/matlabcentral/answers/93586-why-do-i-get-the-subscripted-assignment-dimension-mismatch-error-message

I would encourage you to use the MATLAB Debugger to debug your code and you can figure out yourself what is going wrong.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by