0% found this document useful (0 votes)
31 views

Programas Matlab2

This document contains MATLAB code for several exercises: 1) Code to calculate prime numbers up to a user-specified value. 2) Code to calculate the terms of the exponential series. 3) Code to calculate the terms of the Fibonacci sequence. 4) Code that reads in a vector, writes it forward and backward. 5) Code to calculate the square root of a number. 6) Code to calculate the sum of terms in a series. 7) Code to calculate the area of a circular crown. 8) Code that prints patterns of asterisks in lines. 9) Code that finds the position of a character in a string. 10) Code that determines the type

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Programas Matlab2

This document contains MATLAB code for several exercises: 1) Code to calculate prime numbers up to a user-specified value. 2) Code to calculate the terms of the exponential series. 3) Code to calculate the terms of the Fibonacci sequence. 4) Code that reads in a vector, writes it forward and backward. 5) Code to calculate the square root of a number. 6) Code to calculate the sum of terms in a series. 7) Code to calculate the area of a circular crown. 8) Code that prints patterns of asterisks in lines. 9) Code that finds the position of a character in a string. 10) Code that determines the type

Uploaded by

David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ejercicios Resueltos en Matlab:

n=input ('Introduce the overall number of terms: ')


dividend=2;
count=1;
while (count <=n)
divisor=2;
while mod (dividend, divisor) ~= 0

divisor=divisor + 1;
end
if dividend ==divisor
fprintf('%d ',dividend);

count=1+count;
end
dividend =1+ dividend;
end

display( ' n has to be a number integer and positive ' )


n=input ( ' Introduce the number of sessions terms : ' )
for x=1:1:n
an=(1+(1/x))^x;
fprintf('a%d = %g\n', x, an);

end

Susession de Fibonacci
n=input( ' Number of Fibonacci succession`s terms: ' )
fibonacci=zeros(1,n);
fibonacci(1)=1;
fibonacci(2)=1;
i=3;
while i <= n
fibonacci(i)= fibonacci(i-2)+ fibonacci(i-1);
i=i+1;
end
fprintf('%g ',fibonacci);
fprintf('\n');

% Read the components of a Vector and then write in the right way and in the
reverse way
display( ' Write 5 components of a vector: ' );
a=input(' fist component: ' );
b=input(' second component: ' );
c=input(' third component: ' );
d=input(' fourth component: ' );
e=input(' firth component: ' );
V=[ a b c d e ];
w=zeros(1,5);
for j=1:1:5
w(j)=V(j);
end
display( ' Vector in the right way ' );
fprintf('%g ',w);
fprintf('\n');
U=zeros(1,5);
for i= 5:-1:1
U(i)=V(6-i);
end
display( ' Vector in the opposite way ' )
fprintf('%g ',U);
fprintf('\n');

number=input ('Introduce the number it has to be integer and real ' )


i=1;
while i*i < number
i=i+1;
end
fprintf('%g ',i);

n=input (' Introduce the n term of this serie: ');


m=input ( ' Introduce the m term: ');
u2=0;
for i=1:1:n
u=0;
u=1/(i)^m;
u1=u;
u2=u1+u2;
end
fprintf('%g ',u2);

% compute the sum of terms of the following serie.


n=input ( ' Introduce the n terms of the Serie: ' );
a=zeros(1,n);
a(2)=1;
a(1)=1;
w2=0;
for i=3:1:n
w=0;
a(i)=a(i-1)+a(i-2);
w=1/a(i);
t=w;
w2=t+w2;
end
w3=2+w2;

fprintf('%g ',w3);

% compute the area of a circular crown


r1= input ( ' radio circunferencia 1: ' );
r2= input ( ' radio circunferencia 2: ' );

if r1 == r2,
display( ' El area circular es 0 : ' );

else if r1 < r2
area=pi()*(r2^2-r1^2);

fprintf('%g ',area);
else
area=pi()*(r1^2-r2^2);

fprintf('%g ',area);

end
end

--------------------------------------------------------------------------------------------------------------------------
% create a program that has to write lines of * .
u=input( ' number of elements of the last line: ');
n=1;

while n<u ;

v= zeros (1,n);
v1=num2cell(v);
for i=1:n
v1{i}= ' *' ;
end

v1
n=n+1;
end

%second method to calculate the previous problem, lines of *:

n=input( ' number of rows: ' );

for i=1:1:n
for j=n-i:-1:0

fprintf( '*');

end

fprintf ( ' \n ');

end

n=input(' number of filas : ');


for i=1:1:n

for w= n-i:n-1

fprintf( ' ' );

end
for j=n-i:-1:0
fprintf( ' * ' );
end

fprintf( ' \n ' );

end

% Buscar la primera posicion de una letra en una cadera de caracteres de


Matlab.
n=input( ' Introduce la palabra: ','s' );
u=input( ' Introduce el smbolo a buscar: ','s' );
lp=length(n);
ls=length(u);
for i=1:1:ls
for j=1:1: lp
if u(i) == n(j)
fprintf( 'SMBOLO %s en posicin %d\n',u(i),j);

break;
end
if j==lp

fprintf( 'SMBOLO %s no se encontro %d\n',u(i));

end
end
end

% dados los lados de un tringulo determinar el tipo de triangulo


a=input( ' Introducir la distancia del lado a : ');
b=input( ' Introducir la distancia del lado b: ' );
c= input( ' Introducir la distancia del lado c: ');

if a==b & b==c

display( ' El tringulo es un equiltero con sus tres lados iguales: ');
else if a==b | b==c | c==a
display( ' El tringulo es un issceles con dos de sus tres lados iguales: ');
else
display( ' El tringulo es un escaleno con sus tres lados desiguales: ');
end
end

You might also like