MATLAB Answers

Solve a system of equations iteratively

8 views (last 30 days)
Riccardo Rinaldi
Riccardo Rinaldi on 7 Apr 2018
Answered: Arun Mathamkode on 20 Apr 2018
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

  0 Comments

Sign in to comment.

Answers (1)

Arun Mathamkode
Arun Mathamkode on 20 Apr 2018

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.

  0 Comments

Sign in to comment.