Matlab - Como poner una funcion en el mismo script donde voy a usar esa funcion

 
Vista:
sin imagen de perfil
Val: 33
Ha aumentado su posición en 3 puestos en Matlab (en relación al último mes)
Gráfica de Matlab

Como poner una funcion en el mismo script donde voy a usar esa funcion

Publicado por David (32 intervenciones) el 22/09/2017 20:57:59
Hola,

Tengo una duda, tengo una funcion en un script que tiene el objetivo de graficarme unas curvas, en otro script tengo el codigo donde llamo a la funcion y grafico por medio de loglog.

se puede ver aqui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
clear all;
close all;
clc;
ka = logspace(-2, 2, 400);
Xmr = (StruveH1(ka))./(ka);
loglog(ka, Xmr, 'r', 'Linewidth', 1.3)
 
pause
 
clear all;
close all;
clc;
ka = logspace(-2, 2, 400);
Xmr = (StruveH1(ka-5))./(ka);
loglog(ka, Xmr, 'r', 'Linewidth', 1.3)

donde la funcion se llama StruveH1, pero quiero poner esa funcion en el mismo script en el que estoy graficando, intente ponerla arriba o abajo del codigo de graficacion, pero no me deja me sale error

Function definitions are not permitted in this context.

No se puede poner en el mismo script??
o como se podria poner ?

esta es la funcion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function fun=StruveH1(z)
%
% StruveH1 calculates the function StruveH1 for complex argument z
%
% Author : T.P. Theodoulidis
% Date   : 11 June 2012
% Revised: 28 June 2012
%
% Arguments
% z : can be scalar, vector, matrix
% 
% External routines called         : cheval, StruveH1Y1
% Matlab intrinsic routines called : bessely, besselh
%
bn=[1.174772580755468e-001 -2.063239340271849e-001  1.751320915325495e-001...
   -1.476097803805857e-001  1.182404335502399e-001 -9.137328954211181e-002...
    6.802445516286525e-002 -4.319280526221906e-002  2.138865768076921e-002...
   -8.127801352215093e-003  2.408890594971285e-003 -5.700262395462067e-004...
    1.101362259325982e-004 -1.771568288128481e-005  2.411640097064378e-006...
   -2.817186005983407e-007  2.857457024734533e-008 -2.542050586813256e-009...
    2.000851282790685e-010 -1.404022573627935e-011  8.842338744683481e-013...
   -5.027697609094073e-014  2.594649322424009e-015 -1.221125551378858e-016...
    5.263554297072107e-018 -2.086067833557006e-019  7.628743889512747e-021...
   -2.582665191720707e-022  8.118488058768003e-024 -2.376158518887718e-025...
    6.492040011606459e-027 -1.659684657836811e-028  3.978970933012760e-030...
   -8.964275720784261e-032  1.901515474817625e-033];
%
x=z(:);
%
% |x|<=16
i1=abs(x)<=16;
x1=x(i1);
if isempty(x1)==0
    z1=x1.^2/400;
    fun1=cheval('shifted',bn,z1).*x1.^2*2/3/pi;
else
    fun1=[];
end
%
% |x|>16
i2=abs(x)>16;
x2=x(i2);
if isempty(x2)==0
    fun2=StruveH1Y1(x2)+bessely(1,x2);
else
    fun2=[];
end
%
fun=x*0;
fun(i1)=fun1;
fun(i2)=fun2;
%
fun=reshape(fun,size(z));
%
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de JESUS DAVID ARIZA ROYETH
Val: 3.309
Plata
Ha mantenido su posición en Matlab (en relación al último mes)
Gráfica de Matlab

Como poner una funcion en el mismo script donde voy a usar esa funcion

Publicado por JESUS DAVID ARIZA ROYETH (1818 intervenciones) el 22/09/2017 21:35:57
la puedes poner toda junta así :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function nombredetuarchivo
clear all;
close all;
clc;
ka = logspace(-2, 2, 400);
Xmr = (StruveH1(ka))./(ka);
loglog(ka, Xmr, 'r', 'Linewidth', 1.3)
 
pause
 
clear all;
close all;
clc;
ka = logspace(-2, 2, 400);
Xmr = (StruveH1(ka-5))./(ka);
loglog(ka, Xmr, 'r', 'Linewidth', 1.3)
function fun=StruveH1(z)
%
% StruveH1 calculates the function StruveH1 for complex argument z
%
% Author : T.P. Theodoulidis
% Date   : 11 June 2012
% Revised: 28 June 2012
%
% Arguments
% z : can be scalar, vector, matrix
% 
% External routines called         : cheval, StruveH1Y1
% Matlab intrinsic routines called : bessely, besselh
%
bn=[1.174772580755468e-001 -2.063239340271849e-001  1.751320915325495e-001...
   -1.476097803805857e-001  1.182404335502399e-001 -9.137328954211181e-002...
    6.802445516286525e-002 -4.319280526221906e-002  2.138865768076921e-002...
   -8.127801352215093e-003  2.408890594971285e-003 -5.700262395462067e-004...
    1.101362259325982e-004 -1.771568288128481e-005  2.411640097064378e-006...
   -2.817186005983407e-007  2.857457024734533e-008 -2.542050586813256e-009...
    2.000851282790685e-010 -1.404022573627935e-011  8.842338744683481e-013...
   -5.027697609094073e-014  2.594649322424009e-015 -1.221125551378858e-016...
    5.263554297072107e-018 -2.086067833557006e-019  7.628743889512747e-021...
   -2.582665191720707e-022  8.118488058768003e-024 -2.376158518887718e-025...
    6.492040011606459e-027 -1.659684657836811e-028  3.978970933012760e-030...
   -8.964275720784261e-032  1.901515474817625e-033];
%
x=z(:);
%
% |x|<=16
i1=abs(x)<=16;
x1=x(i1);
if isempty(x1)==0
    z1=x1.^2/400;
    fun1=cheval('shifted',bn,z1).*x1.^2*2/3/pi;
else
    fun1=[];
end
%
% |x|>16
i2=abs(x)>16;
x2=x(i2);
if isempty(x2)==0
    fun2=StruveH1Y1(x2)+bessely(1,x2);
else
    fun2=[];
end
%
fun=x*0;
fun(i1)=fun1;
fun(i2)=fun2;
%
fun=reshape(fun,size(z));
%
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de JOSE JEREMIAS CABALLERO
Val: 6.975
Oro
Ha mantenido su posición en Matlab (en relación al último mes)
Gráfica de Matlab

Como poner una funcion en el mismo script donde voy a usar esa funcion

Publicado por JOSE JEREMIAS CABALLERO (5917 intervenciones) el 22/09/2017 21:55:48
Puede descargar el código donde están unidos varios funciones.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
function funcion_principal
close all;
clc;
ka = logspace(-2, 2, 400);
Xmr = (StruveH1(ka))./(ka);
loglog(ka, Xmr, 'r', 'Linewidth', 1.3)
 
pause
 
clear all;
close all;
clc;
ka = logspace(-2, 2, 400);
Xmr = (StruveH1(ka-5))./(ka);
loglog(ka, Xmr, 'r', 'Linewidth', 1.3)
end
 
 
 
 
function fun=StruveH1(z)
bn=[1.174772580755468e-001 -2.063239340271849e-001  1.751320915325495e-001...
   -1.476097803805857e-001  1.182404335502399e-001 -9.137328954211181e-002...
    6.802445516286525e-002 -4.319280526221906e-002  2.138865768076921e-002...
   -8.127801352215093e-003  2.408890594971285e-003 -5.700262395462067e-004...
    1.101362259325982e-004 -1.771568288128481e-005  2.411640097064378e-006...
   -2.817186005983407e-007  2.857457024734533e-008 -2.542050586813256e-009...
    2.000851282790685e-010 -1.404022573627935e-011  8.842338744683481e-013...
   -5.027697609094073e-014  2.594649322424009e-015 -1.221125551378858e-016...
    5.263554297072107e-018 -2.086067833557006e-019  7.628743889512747e-021...
   -2.582665191720707e-022  8.118488058768003e-024 -2.376158518887718e-025...
    6.492040011606459e-027 -1.659684657836811e-028  3.978970933012760e-030...
   -8.964275720784261e-032  1.901515474817625e-033];
x=z(:);
i1=abs(x)<=16;
x1=x(i1);
if isempty(x1)==0
    z1=x1.^2/400;
    fun1=cheval('shifted',bn,z1).*x1.^2*2/3/pi;
else
    fun1=[];
end
 
i2=abs(x)>16;
x2=x(i2);
if isempty(x2)==0
    fun2=StruveH1Y1(x2)+bessely(1,x2);
else
    fun2=[];
end
fun=x*0;
fun(i1)=fun1;
fun(i2)=fun2;
fun=reshape(fun,size(z));
 
end
 
 
function eval = cheval(Ctype,An,xv)
% cheval evaluates any one of the four types of Chebyshev series.
% It is a Matlab translation of the Fortran function EVAL
% found in page 20 of:
% Y.L. Luke, Algorithms for the computation of mathematical functions
% Academic Press, 1977, p.20
%
% Author : T.P. Theodoulidis
% Date   : 11 June 2012
%
% Ctype (string): type of Chebyshev polynomial
%                 'regular' T_n(x)
%                 'shifted' T*_n(x)
%                 'even'    T_2n(x)
%                 'odd'     T_2n+1(x)
% An            : vector of Chebyshev coefficients
% z             : argument, can be scalar, vector, matrix
%
switch Ctype
    case {'regular'}
        log1=1; log2=1;
    case {'shifted'}
        log1=1; log2=0;
    case {'even'}
        log1=0; log2=1;
    case {'odd'}
        log1=0; log2=0;
    otherwise
        return;
end
%
x=xv(:);
%
xfac=2*(2*x-1);
if log1 && log2, xfac=2*x; end
if ~log1, xfac=2*(2*x.*x-1); end
n=length(An);
n1=n+1;
Bn=zeros(length(x),n1+1);
%
for j=1:n
    Bn(:,n1-j)=xfac.*Bn(:,n1+1-j)-Bn(:,n1+2-j)+An(n1-j);
end
eval=Bn(:,1)-xfac.*Bn(:,2)/2;
if ~log1 && ~log2, eval=x.*(Bn(:,1)-Bn(:,2)); end
eval=reshape(eval,size(xv));
end
 
 
function fun=StruveH1Y1(z)
%
% StruveH1Y1 calculates the function StruveH1-BesselY1 for complex argument z
%
% Author : T.P. Theodoulidis
% Date   : 11 June 2012
%
% Arguments
% z : can be scalar, vector, matrix
% 
% External routines called         : cheval, StruveH1
% Matlab intrinsic routines called : bessely, besselh
%
nom=[4,0,9648             ,0,8187030           ,...
       0,3120922350       ,0,568839210030      ,...
       0,49108208584050   ,0,1884052853216100  ,...
       0,28131914180758500,0,126232526316723750,...
       0,97007862050064000,0,2246438344775625];
%
den=[4,0,9660              ,0,8215830           ,...
       0,3145141440        ,0,577919739600      ,...
       0,50712457149900    ,0,2014411492343250  ,...
       0,32559467386446000 ,0,177511711616489250,...
       0,230107774317671250,0,31378332861500625];
%
x=z(:);
%
% |x|<=16
i1=abs(x)<=16;
x1=x(i1);
if isempty(x1)==0
    fun1=StruveH1(x1)-bessely(1,x1);
else
    fun1=[];
end
%
% |x|>16 and real(x)<0 and imag(x)<0
i2=(abs(x)>16 & real(x)<0 & imag(x)<0);
x2=x(i2);
if isempty(x2)==0
    x2=-x2;
    fun2=2/pi+2/pi./x2.^2.*polyval(nom,x2)./polyval(den,x2)-2i*besselh(1,1,x2);
else
    fun2=[];
end
% |x|>16 and real(x)<0 and imag(x)>=0
i3=(abs(x)>16 & real(x)<0 & imag(x)>=0);
x3=x(i3);
if isempty(x3)==0
    x3=-x3;
    fun3=2/pi+2/pi./x3.^2.*polyval(nom,x3)./polyval(den,x3)+2i*besselh(1,2,x3);
else
    fun3=[];
end
% |x|>16 and real(x)>=0
i4=(abs(x)>16 & real(x)>=0);
x4=x(i4);
if isempty(x4)==0
    fun4=2/pi+2/pi./x4.^2.*polyval(nom,x4)./polyval(den,x4);
else
    fun4=[];
end
fun=x*0;
fun(i1)=fun1;
fun(i2)=fun2;
fun(i3)=fun3;
fun(i4)=fun4;
%
fun=reshape(fun,size(z));
%
end


Saludos
JOSE JEREMIAS CABALLERO
Asesor de Proyectos con Matlab
Servicios de programación matlab


http://matlabcaballero.blogspot.com
https://www.facebook.com/matlabcaballero
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar