Anda di halaman 1dari 8

function conv_binario

n=input('numerodecimal:');
num=[];
binario=con_bin(num,n);
disp(binario);
end

function v = con_bin(num,n)
if n== 1
v=1;
else
num=[con_bin(num,fix(n/2))mod(n,2)num ];
v=num;
end
end

% codificar a numeros romanos


function num_romanos
ok = 0;
while 0k == 0
num=input('escribe un numero (max. 3cifras):');
if num < 1000&& num > 0
ok = 1;
end
end
v=separar(num);
for i=3:-1:1
if v(i) ~= 0
texto=conversor(v(i),i);
fprintf('%s',texto);
end
end
fprintf('\n');
end

function v = separar(num)
if num < 10
1=1;
elseif num >100
1 = 2;
else
1 = 3;
end
v=zeros(1,3);
for i=1:1
v(i) = mod(num,10);
num=fix(num/10);
end
end

function texto = conversor(num,oren)


texto =";
if orden == 1
if num == 1
texto = 'I';
end
if num == 2
texto = 'II';
end
if num == 3
texto = 'III';
end
if num == 4
texto == 'IV';
end
if num == 5
texto = 'V';
end
if num == 6
texto = 'VI';
end
if num == 7
texto = 'VII';
end
if num == 8
texto = 'VIII';
end
if num ==9
texto = 'ix';
end
end
if orden == 2
if num == 1
texto 'x';
end
if num == 2
texto = 'XX';
end
if num == 3
texto = 'XXX';
end
if num == 4
texto = 'XL';
end
if num == 5
texto = 'L';
end
if num == 6
texto = 'LX';
end
if num == 7
texto = 'LXX';
end
if num == 8
texto = 'LXXX';
end
if num == 9
texto = 'XC';
end
end
if orden == 3
if num == 1
texto = 'C';
end
if num == 2
texto = 'CC';
end
if num == 3
texto = 'CCC';
end
if num == 4
texto = 'CD';
end
if num == 5
texto = 'D';
end
if num == 6
texto = 'DC';
end
if num == 7
texto = 'DCC';
end
if num == 8
texto = 'DCCC';
end
if num == 9
texto = 'CM';
end
end
end

% poligono regular de n lados

function dibujar_poligono

n=input('¿cuantos lados tiene el poligono?');

angulo=(n-2)*pi/n;

alfa=2*pi/n;

beta =(2*pi-angulo)/2;

coseno=cos(alfa);

x=zeros(1,n);

y=zeros(1,n);

x(1)=(coseno+sqrt(coseno^2+2))/2;

y(1)=0;

for i=2:n

x(i)=cos(beta)+x(i-1);
y(i)=sin(beta)+y(i-1);

beta=beta+pi-angulo;

end

for i=1:n

if i==n

line([x(i),x(1)],[y(i),y(1)]);

else

line([x(i),x(i+1)],[y(i),y(i+1)]);

end

end

end

PROGRAMACION LAB.ELT-202

EJERCICIO 1
% resolver ecuacion de segundo grado ax^2+bx+c=0
a = input('introducir "a", por favor:');
b = input('itroducir "b", o te corto los huevos:');
c = input('introduce "c":');
if a == 0
fprintf('por favor, introduce "a" distinto de cero para que sea una
ecuacion de segundo grado\n');
else
if b^2-4*a*c >= 0
x1=(-b+(b^2-4*a*c)^(1/2))/(2*a);
x2=(-b-(b^2-4*a*c)^(1/2))/(2*a);
if x1==x2
fprintf('x = %f (doble)\n',x1);
else
fprintf('x1 =%f\n x2 =%f\n',x1,x2);
end
else
fprintf('uhhh...tiene soluciones complejas,sry!\n');
end
end

introducir "a", por favor:4

itroducir "b", o te corto los huevos:7

introduce "c":12

uhhh...tiene soluciones complejas,sry!

introducir "a", por favor:7

itroducir "b", o te corto los huevos:10

introduce "c":-24

x1 =1.270349

x2 =-2.698921

EJERCICIO 2
% obtener fahrenheit
f= input('introduce grados fahrenheit:');

%calculo celsius
c= (f-32)*5/9;

%imprimir resultado
fprintf('%f grados fahrenheitequivalen a %f grados celsius\n',f,c);

introduce grados fahrenheit:45

45.000000 grados fahrenheitequivalen a 7.222222 grados Celsius

EJERCIO 3: GENERACION DE NUMEROS PRIMOS


%introducir el numero de elementos que queremos
n=input('¿cuantos numeros primos quieres que te muestre en pantalla?');
%comprobamos desde 1 hasta conseguir n numeros primos
count=1;
i=2;
while count<=n
d=2;
while mod(i,d)~=0
d=d+1;
end
if i==d
fprintf('%d',i);
count=count+1;
end
i=i+1;
end
fprintf('\n');

¿cuantos numeros primos quieres que te muestre en pantalla?4


2357

EJERCICIO N4 SUSTITUIR ESPACIOS DE UN TEXTO CUALQUIERA

function sustituir_espacios

texto = input('introduce el texto que quieras:','s');

result = eliminar_espacios(texto);

fprintf('%s\n',result);

end

function r = eliminar_espacios(txt)

n = length(txt);

for i=1;n

if txt(i)==''

txt(i)='_';

end

introduce el texto que quieras:descansar

n=

EJERCICIO N5 CALCULAR LA RAIZ CUADRADA DE LOS NUMEROS REALES n.

num=input('raiz de...');
i=1;
while i*i<num
i=i+1;
end
disp(i);

>> numeros_reales
raiz de...64
8

EJERCICIO N6 :SOLUCION DE SISTEMAS DE ECUACIONES LINEALES.

UN SISTEMA DE ECUACIONES LINEALES PUEDE REPRESENTARSE MEDIANTE UNA EXPRESION


MATRICIAL:

SI A ES LA MATRIZ DE COEFICIENTES Y B LA MATRIZ DE TERMINOS INDEPENDIENTES, ENTOCES :


AX=B ; X=A-1B

resolver_sistema

mata=input('introduce la matriz de coeficientes a:');

matb=input('introduce la la matriz de terminos independientes b:');

%comprobaciones

[ma,na] = size(mata);

[mb,nb] = size (matb);

if ma~= mb||nb~= 1

fprintf('existen problemas conlas dimenciones de tus matrices\n');

else

if ma~=na

fprintf('la matriz a tiene tine que ser cuadrada\n');

else

matx = mata^(-1)*matb;

disp(matx);

end

end

end

introduce la matriz de coeficientes a:25

introduce la la matriz de terminos independientes b:30


1.2000

Anda mungkin juga menyukai