I have a function statement that works in Octave but fails in Matlab

조회 수: 4 (최근 30일)
Geoff Taylor
Geoff Taylor 2019년 12월 29일
편집: Stephen23 2019년 12월 30일
my code in Octave is
for j= 1:12
I(j)= Io +j*DI;
j
x
[x ,fval] = fsolve(@(x) f(x,I(j)), x0, options);
y1(j)= x(1)
y2(j)= x(2)
x0 = x
endfor
plotyy (I , y1 , I , y2 )
function [y] = f( x , I )function [y] = f( x , I )
MAtlab says
Error: File: CurrentagainstT.m Line: 20 Column: 1
Function definition not supported in this context. Create functions in code file.
What am I supposed to do
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 12월 30일
And you appear to "function" the same thing twice in one line.
Geoff Taylor
Geoff Taylor 2019년 12월 30일
편집: Stephen23 2019년 12월 30일
Here is my complete code below. The word "function" only appears once on the line, and both "for" loop and "function" module end with "end" statement. This code works well in GNU Octave. However I get the MATLAB error
Error: File: CurrentagainstT.m Line: 97 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "f" function definition to before the first local function definition.
options=optimset('Display','iter');
x0=[310 ; 0.001 ]; Io=1e-3; DI=1.5e-3;
maxiter = 12;
x = zeros(1, maxiter);
fval = zeros(1, maxiter);
for j= 1:12
I(j)= Io +j*DI;
j
x
[x ,fval] = fsolve(@(x) f(x,I(j)), x0, options);
y1(j)= x(1)
y2(j)= x(2)
x0 = x
end
plotyy (I , y1 , I , y2 )
function y = f( x , I )
m=5 ; D=0.9e-3; E1n=0.03 ; E1p=0.01 ; phit =0 ; phib =0; m1 = 3 ; ar=5e-4;
co=3e10 ; lamda=0.98e-4 ; ngaas= 3.6; gamma=0.069; ngo=3.27;
To=300;Rdo=80;
R=0.997;alpha=15;Lc=0.4298e-4;q=1.6e-19; vg=8e9;
h=6.6e-34;mo=9e-31;k=1.38e-23;hb=h/2/pi;kb=k/q;kT=k*To;
Nc=5e17;Nv=7e18;xn=1e-5;xp=1e-5;Aein = 1.5e-10;
Bbar= (Aein/(8*pi))*(lamda/ngaas)^2*(co/ngaas);
mn=0.067;mp=0.45;Dp=1e-4*mp*mo/pi/hb^2;Dn=1e-4*mn*mo/pi/hb^2;Lz=0.6e-6;
Dnp=Dn*Dp ;
tauinv=vg*(log(1/R)/Lc + alpha); taup =1/tauinv;
taupp=gamma*taup;
Kpar = Lz^2/(m*taupp*h*Dnp*Bbar*kT) ;
z1= 2*exp(2*Kpar);
EFtm=2*(kT/q)*log(z1-1+sqrt((z1-1)^2-1)) ;
dEg=0.3;EFn=EFtm/2;EFp=EFtm/2; Ego= 1.30 -5.6e-4 *To^2/(204+To);
Egeff= E1n +E1p + Ego;
ng = ngo+ar*To ;
hv1 = h*co*(m1-(phit+phib))/(2*q*Lc*ng) ;
hv= hv1 - Egeff ;
Vj=Egeff+EFtm ;
nth=Dn*EFn*q/Lz ;
t1=x(1);
Rd = Rdo +0.083*(x(1)-To);
A=(pi/4)*D.^2;
tnr=1e-9;q=1.6e-19;Jqw=q*m*Lz*nth/tnr;
Rjo= 2000;Rjp=5 ;Rj= Rjo + Rjp*(x(1)-To);
Jtr=q*((xn+xp)/tnr/2)*sqrt(Nc*Nv)*exp(-(dEg-(E1n+E1p)-EFtm)/(2*kb*x(1)))
Jth=Jtr+Jqw;Ith=A*Jth
etae=Jqw/Jth ;
etao=log(1/R)/(alpha*Lc+log(1/R));
etas= (Egeff+hv)*etae*etao;
t2=I
delT=(I.^2*Rd+I*Vj-x(2))*Rj
y(1) = x(1) - To - delT
y(2) = x(2)-(I-Ith)*etas
x(1)
x(2)
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2019년 12월 29일
In most instances, only anonymous functions can occur in a script file in MATLAB.
The documentation section on Create Functions in Files notes tthat:
  • Starting in R2016b, another option for storing functions is to include them at the end of a script file.
so it is possible, with some restrictions. See the cited (and related) documentation for details.

카테고리

Help CenterFile Exchange에서 Octave에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by