Clear Filters
Clear Filters

Subscript indices Error for a Plot

1 view (last 30 days)
DIP
DIP on 20 Apr 2017
Edited: DIP on 20 Apr 2017
Hi I am trying to calculate power but I cant seem to find out what is wrong (im a novice in matlab) The error is in the line for P_out.
%%An example of Matlab code
clc;clear;
format long;
%%Constants
hf_H2 = 0;
hf_O2 = 0;
hf_H2Og = -244820;
sf_H2 = 130.68;
sf_O2 = 205.14;
sf_H2Og = 188.72;
cp_H2O = 33.7;
cp_H2 = 29.5;
cp_O2 = 329.0;
Tref = 298;
Ru = 8.314;
T = 353;
n = 2;
F = 96487;
p_H2 = 1;
p_O2 = 0.21;
a_H2O = 1;
po = 1;
delT = T-Tref;
%%Calculate Nerst Reversible Cell Potential
% Calculate Gibbs Free Energy
delH = hf_H2Og+cp_H2O*delT - (hf_H2+cp_H2*delT) - 0.5*(hf_O2+cp_O2*delT);
delS = sf_H2Og+cp_H2O*log(T/Tref) - (sf_H2+cp_H2*log(T/Tref)) - 0.5*(sf_O2+cp_O2*log(T/Tref));
delG = delH - (Tref * delS);
% Reversible voltage
Ero = -delG/(n*F);
% Nerst Expression for voltage
Eo=Ero+(Ru*T/n/F)*log((p_H2/po)*(p_O2/po)^0.5);
%%Populating matrix for calculations
V_mdl = @(a,I)(Eo-Ru*T/F*log((I+a(4))/a(1))+Ru*T/2./F*log((a(2)-(I+a(4)))/a(2))-I*a(3));
I_exp = [0,2.5,5,10,20,40,60,80,100,200,400,600,800,1000,1200];
V_exp = [0.986615,0.950774,0.951704,0.916295,0.910731,0.90338,0.857798,0.853067,0.851497,0.799984,...
0.730907,0.689608,0.638525,0.57848,0.440924];
Irange = min(I_exp):1:max(I_exp);
%%Initial guess & Brute-force minimize error function
a(1)=0.5; %Io: Reference exchange current density mA/cm2
a(2)=1200.00; %I_lim: Limiting current density mA/cm2
a(3)=2.5e-4; %total resistance kohm*cm2
a(4)=21.0; %cross-over current density mA/cm2
g = @(a) norm(V_mdl(a,I_exp)-V_exp);
ahat = fminsearch(g,a); % Values Calculated
V_cell = @(I) V_mdl(ahat,I);
%%Plotting and figures
hold on
figure(1)
scatter(I_exp,V_exp,'ok','MarkerFaceColor','b')
plot(Irange,V_cell(Irange),'r','linewidth',2)
grid on
title('Cell Voltage vs Current Density')
xlabel('Current Density [i, mA/cm^2]')
ylabel('Voltage [\eta, V]')
legend('-- Polarization Curve')
%%Calculation of power
I_t=I_exp';
_P_out(I_exp) = (V_cell(I_exp))* I_t;_
figure(2)
title('Fuel cell power')
xlabel('Current density (A/cm^2)');
ylabel('Power(Watts)');
plot(I_exp,P_out,'*');
grid on
hold on
  1 Comment
DIP
DIP on 20 Apr 2017
The error is in the 8th line from the end of the code.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 20 Apr 2017
Replace:
P_out(I_exp) = (V_cell(I_exp))* I_t;
With
P_out = (V_cell(I_exp))* I_t;
Your I_exp are real numbers, you cannot use real numbers like indices. Only +ve integers are used to index arrays/ matrices.
  2 Comments
DIP
DIP on 20 Apr 2017
Thanks a ton. How can I extract values from the function handle ?
DIP
DIP on 20 Apr 2017
Edited: DIP on 20 Apr 2017
Also I get a wrong plot. Power = Vcell * i . But im unable to get the correct graph.
What I got
What i ought to get is somewhat a curve (x y axis would be different as the pic below is just and example)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!