Sei sulla pagina 1di 26

FUNCIONES DEFINIDAS

POR EL USUARIO

Cos(x)
se llama cos,
toma la entrada del usuario dentro de parntesis (en este
caso, x), y
calcula un resultado.

function salida =
mi_funcin(x)
Y= 2X

la palabra function,
una variable que defina la salida de funcin,
un nombre de funcin, y
una variable que se use para el argumento de entrada.

salida

function salida =
mi_funcin(x)
function resultado = polinomio( x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
resultado= 2*x.^2+ 2*x+1;

>> polinomio(2)

>> x= [ 1 2 3 4 5 6]
x=
1

>> polinomio(x)
ans =
5

13

25

41

61

85

61

85

>> y= [ 1 2 3 4 5 6];
>> polinomio(y)
ans =
5

13

25

41

jerciciodeprctica
E
Cree funciones MATLAB para evaluar las siguientes funciones matemticas
(asegrese de seleccionar nombres de funcin significativos):
Y(x) = x^2
Y(x) =
Y(x)= seno(x^2)
Cree funciones MATLAB para las siguientes conversiones de unidades (es posible
que
necesite consultar un manual o Internet para los factores de conversin
adecuados):
4. pulgadas a pies
5. caloras a joules
6. watts a BTU/h
7. metros a millas
8. millas por hora (mph) a pies/s

function [ y1, y2, y3 ] = m( x )


%UNTITLED Summary of this function goes here
% Detailed explanation goes here
y1= 2*x;
y2=3*x;
y3= 3*x;
end
En la ventana de comandos escriba:
[y1, y2, y3]=m(2)
[f, g, h]=m(2)
m(2)

ego de haber ejecutado los 3 tems escriba sus conclusiones:

En la ventana de comandos escriba:


>> x=5
resumen=[ x', y1', y2', y3']

y1= x+y;
end

>> x=1:10;
[f, g, h]=m(x)

m(2,3)

>> columnas=[ x', f', g', h']


escriba sus conclusiones:

function [ y1 ] = m( x,y )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here

function [ y1, y2 ] = m( x,y )


%UNTITLED Summary of this function goes here
% Detailed explanation goes here
y1= x+y;
y2= x-y;
end

>> [z1, z2]=m(9,5) [y1, y2]=m(9,5)

Tambin puede crear funciones que regresen ms de una variable de salida

function [ y1 ] = m( x,y )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here

function [ y1, y2 ] = m( x,y )


%UNTITLED Summary of this function
goes here
% Detailed explanation goes here

y1= x+y;
end

n la ventana de comandos escriba:


m(2,3)

escriba sus conclusiones:

y1= x+y;
y2= x-y;
end
En la ventana de comandos escriba:
[y1, y2]=m(9,5)

>> [z1,
z2]=m(9,5)
escriba sus conclusiones:

cree y ponga a prueba funciones MATLAB para evaluar las siguientes funciones matemticas
simples con vectores de entrada mltiple y un vector de salida sencilla:

cree y ponga a prueba funciones MATLAB para evaluar las siguientes funciones matemticas
simples con un vector de entrada sencilla y vectores de salida mltiple:

cree y ponga a prueba funciones MATLAB para evaluar las siguientes funciones matemticas
simples con vectores de entrada mltiple y un vectores de salida multiple:

function [ ] = m( )
% mi primera funcion
%
Detailed explanation goes here
teta=pi/2:0.8*pi:4.8*pi;
r=ones(1,6);
polar(teta,r)
end

En la ventana de comandos escriba:


>> m

a = clock
Help m

Determinacin del nmero de argumentos de entrada y salida


>> nargin('sin')
ans =
1
>> nargin('rem')
ans =
2
>> nargin('surf')
ans =
-1
>> nargout('sin')
ans =
1

Variables locales
function [ r ] = multiplica( x, y )
% Esta funcin multiplica dos nmeros que
deben ser ingreados entre parntesis y
separados por coma
r= x*y;
end
>> clc, clear
>> multiplica(3,3)
>> x
>> y
>> r
>> nargin('multiplica')
ans =
2
>> nargout('multiplica')
ans =
1

Variables locales
function [ r ] = f( t )
%UNTITLED2 Summary of this function goes
here
%
Detailed explanation goes here
r= 0.5*g.*t^2;
end

>> clear, clc


>> g=9.8
g=
9.8000
>> f(2)

function [ r ] = f( t )
%UNTITLED2 Summary of this function goes
here
%
Detailed explanation goes here
g=9.8;
r= 0.5*g.*t^2;
end
>> f(3)

Variables locales
function [ r ] = f( t )
%UNTITLED2 Summary of this function goes
here
%
Detailed explanation goes here
g=9.8;
r= 0.5*g.*t^2;
end

>> f(3)

Variables locales

function [ r ] = f( t,g )
%UNTITLED2 Summary of this function goes
here
%
Detailed explanation goes here
r= 0.5*g.*t^2;
end

>> f(3, 9.8)


ans =
44.1000

Variables globales
function [ r ] = f( t )
%UNTITLED2 Summary of this function goes
here
%
Detailed explanation goes here
global G;
r= 0.5*G.*t^2;
end

>>
G=9.8;
>> f(3)

>> global G;
>> G=9.8;

>> G=2;
>> f(3)

Acceso a cdigo de archivo-m

>>

type f

>> type ('f')

>> type

sphere

FUNCIONES ANNIMAS

>> d=@(x) x^2;


>> d(2)

>> w= @(x,y)
x^2+y^2;
>> w(2,2)

>>
>>
>>
>>

y=@ sin
y(1)
sin(1)
fplot(y, 0:1)

Acaso la ecuacin ms famosa en fsica sea


E = mc2
que relaciona la energa E con la masa m. La rapidez de la luz en el vaco, c, es la
propiedad que vincula a las dos. La rapidez de la luz en el vaco es 2.9979 x 10^8 m/s.
(a) Cree una funcin llamada energy para encontrar la energa correspondiente a una
masa dada en kg. Su resultado estar en joules, pues 1 kg m2/s2 = 1 joule.
(b) Use su funcin para encontrar la energa correspondiente a masas desde 1 kg
hasta 10 ^6 kg. Use la funcin logspace (consulte help/logspace) para crear un
vector masa adecuado.
(c) Cree una grfica de sus resultados. Intente usar diferentes enfoques de graficacin
logartmica (por ejemplo: semilogy, semilogx y loglog) para determinar la mejor
forma de graficar sus esultados.

Potrebbero piacerti anche