Info

This question is closed. Reopen it to edit or answer.

can anyone help me to fixed this error in my code?

1 view (last 30 days)
Index exceeds matrix dimensions.
Error in fun (line 33)
f(1) = C_avg(i) - Cin(i) + (2 * J(i) /R * dtau);
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Error in main (line 21)
answ = fsolve(@fun,x0,options);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
>>
function f = fun(x0)
global tau_max N_tau x_max R N_x dx K1 K2
%initial guess
C_avg =x0(1);
J =x0(2);
C_avg = zeros(N_tau,1); %prolocating raw vector
C = zeros(1,N_x); %prolocating colum vector
% C_avg is average value of colum vector which is given by equation f(2)
%C is the coulmn vector values
for i = 2:N_tau
for k =1:N_x
if i ==2
Cin(i) =208;
else
Cin(i) = C_avg(i-1);
end
dtau = tau_max/ (N_tau -1);
f(1) = C_avg(i) - Cin(i) + (2 * J(i) /R * dtau);
f(2) = C_avg(i) - 2 * (sum ( C(i,k)) - C(1,1) - C(1,N_x)); %where C(i,k) is a column vector [N_tau,N_x] and k is 1 to N_x
f(3) = J(i) + K1 * (C(i,N_x) - C(i,1)); % difference between first value of colum vector and last value colum vector.
f(4) = J(i) - K2*C(i,N_x);
end
end
end
%%script
clc;
global tau_max N_tau x_max R N_x dx K1 K2
options=optimset('Display','off');
tau_max =25;
N_tau = 26;
x_max = 0.003;
R = x_max;
N_x = 5;
dx = x_max/(N_x-1);
K1 = 4.42e-6;
K2 = 4.87e-5;
tau =linspace(0,tau_max,N_tau);
x =linspace(0,R,N_x);
x0 = [193.44,0.007];
answ = fsolve(@fun,x0,options);
figure(1)
plot (C_avg,tau)
xlabel('C_avg')
ylabel('tau')
figure(2)
plot(J,tau)
xlabel('J')
ylabel('tau')

Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!