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

MP1 Notes

The document contains MATLAB code that performs several tasks: 1) It defines functions to calculate Fibonacci numbers and factorials recursively. 2) It contains code to plot various graphs and visualize data using functions like plot, semilogx, loglog, polar, mesh, surf. 3) It includes code to shuffle arrays, reverse digit order of numbers, calculate the discriminant of a quadratic equation, and find the maximum product of 3 consecutive elements in an array. 4) It prompts the user for input and displays corresponding output based on given conditions in the code.

Uploaded by

Ludho Madrid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

MP1 Notes

The document contains MATLAB code that performs several tasks: 1) It defines functions to calculate Fibonacci numbers and factorials recursively. 2) It contains code to plot various graphs and visualize data using functions like plot, semilogx, loglog, polar, mesh, surf. 3) It includes code to shuffle arrays, reverse digit order of numbers, calculate the discriminant of a quadratic equation, and find the maximum product of 3 consecutive elements in an array. 4) It prompts the user for input and displays corresponding output based on given conditions in the code.

Uploaded by

Ludho Madrid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

score=input('Enter raw score in ChE 26: ');

if score > 100


disp(['Weh paano ka nagoverperfect?']);
else
if score <= 100 && score >=92
grade=1.00;
elseif score >= 88
grade=1.25;
elseif score >= 84
grade=1.50;
elseif score >= 80
grade=1.75;
elseif score >= 76
grade=2.00;
elseif score >= 72
grade=2.25;
elseif score >= 68
grade=2.50;
elseif score >= 64
grade=2.75;
elseif score >= 60
grade=3.00;
else
grade=5.00;
disp(['AWWWWWWW']);
end
disp(['Grade: ', num2str(grade)]);
end
for a = 1:20
for b = a:20
for c = b:20
if a^2 + b^2 == c^2
disp([a, b, c]);
else
disp([a, b, c]);
end
end
end
end
length=input('\nEnter positive integer: ');
if length > 0
X=zeros(0,1);
Y=zeros(0,1);
for a = 1:length
code=ceil(rand()*4);
if code == 1

X(a)='A';
Y(a)='G';
elseif code == 2
X(a)='G';
Y(a)='A';
elseif code == 3
X(a)='T';
Y(a)='C';
elseif code == 4
X(a)='C';
Y(a)='T';
end
end
disp(char(X));
disp('Complementary strand: ');
disp(char(Y));
else
disp(['invalid'])
end
a=7:5:127;
b=(0:24).^(1.1);
c=1:25;
c=c.*(c+1)./(2);
d=1:25;
d=d./(d+1);
e=1:25;
e=1./(e.*(e+1));
f=1:25;
f=f.*((-1).^(f-1));
g=1:25;
g(1:2:25)=2.^((g(1:2:25)-1)/2);
g(2:2:25)=2*g(2:2:25)/2+1;
h=0:11;
h=[2.^h, h*2+3];
disp(a);
disp(b);
disp(c);
disp(d);
disp(e);

disp(f);
disp(g);
disp(h);
X=zeros(1,25)+7;
for a = 2:25
X(a)=X(a-1)+5;
end
disp(X);
Y=ones(1,25);
for a = 2:25
Y(a)=Y(a-1)*1.1;
end
disp(Y);
Z=ones(1,25);
for a=2:25
Z(a)=Z(a-1)+a;
end
disp(Z);
H=ones(1,25);
for a=1:25
H(a)=a/(a+1);
format rat;
end
disp(H);
K=1:25;
for a=1:25
K(a)=1/((a)*(a+1));
end
disp(K);
J=1:25;
for a=1:25
J(a)=J(a)*(-1)^(a-1);
end
disp(J);
JayR=1:25;
for a=1:2:25
JayR(a)=2^((JayR(a)-1)/2);
JayR(a+1)=((a-1)/2)*2+3;
end
disp(JayR);
%challenge2

clear,clc
N=input('N: ');
R=input('R: ');
C=input('C: ');
for i=1:N
for j=1:N
if abs(R-i) <= 1 && abs(C-j) <= 1
X(i,j)= 2;
else
X(i,j)= 1;
end
end
end
X(R,C)=1;
disp(X);
%challenge1
N=input('N: ');
R=input('R: ');
C=input('C: ');
A=ones(N,N);
A(R:N,C:N)=2;
disp(A)
%problem5
R=input('Enter what row: ');
C=input('Enter what column: ');
for i=1:R
for j=1:C
la=mod((i-1)*C +j-1, 10);
fprintf('%d', la);
end
fprintf('\n');
end
%Problem1
N=input('Enter whole number: ');
for i=1:N
for j=1:N
if i<=j
X(i,j)='*';
else
X(i,j)='.';
end
end
end
disp(char(X))

%Problem3
for i=1:N
for j=1:N
if i == 1 || i == N || j == 1 || j == N
Y(i,j)=2;
else
Y(i,j)=1;
end
end
end
disp(Y)
%Problem6
for i=1:N
for j=1:N
if mod(i,2)==1;
if mod(i-j,2)==0;
A(i,j)='*';
else
A(i,j)='.';
end
else
A(i,j)='.';
end
end
end
disp(char(A));
%Problem4
R=input('Enter what row: ');
C=input('Enter what column: ');
for i=1:N
for j=1:N
if i==R || j==C
Z(i,j)='*';
else
Z(i,j)='.';
end
end
end
disp(Z);
%Problem2
for i=1:N
for j=1:N
if i >= R && j >= C
B(i,j)=2;

else
B(i,j)=1;
end
end
end
disp(B);
Plotting Notes
x1=linspace(0,6*pi);
x2=linspace(0,6*pi,15);
plot(x1, sin(x1), x2, sin(x2));
x=-2:0.01:10; clf;
y=sin(x);
plot(x,y,'r'); hold on;
y=sin(2*y);
plot (x,y, 'm');
y=sin(2*sin(2*y));
plot(x,y,'b'); hold off;
axis([-2 10 -1.5 1.5]);
P=linspace(10,1000);
T=P.^2;
subplot(1,4,1);
plot(P,T); title('Linear Axes'); grid on;
subplot(1,4,2);
semilogx(P,T); title('SemilogX'); grid on;
subplot(1,4,3);
semilogy(P,T); title('SemilogY'); grid on;
subplot(1,4,4);
loglog(P,T); title('Log-Log Plot'); grid on;
t=0:0.02:6*pi;
r=2*cos(4*t/3);
polar(t,r,'>k');
[x,y]=meshgrid(-8:0.05:8);
r=sqrt(x.^2+y.^2);
z=sin(r)./r;
mesh(x,y,z);
surf(peaks);
axis equal;
axis off;
axis on;
axis auto;

x=[0:0.2:5];
y=2.^x-x.^2-2;
plot(x,y,'--r','LineWidth',1);
title('Ludho is hot');
xlabel('oh yeah');
ylabel('\tau \alpha');
legend('1.00');
grid on;
axis tight;
gtext('ofdsfhs');
function F = Fibonacci(x,y,z)
if z == 2
F = y;
elseif z == 1;
F = x;
else
F = Fibonacci(x,y,z-1) + Fibonacci(x,y,z-2);
end
end
function F = fact(x)
if x == 0
F=1;
else
F=x*fact(x-1);
end
end
Shuffling arrays - BUILT IN randperm(length(A))
Problem:
1. Write a function that prompts the user for an
input number N. If N has more than 1 digit AND
is not a multiple of 10, then output a new
number with the digits of N reversed. If it does
not satisfy this condition, then continue asking
the user for an input again. Note that this
function does not accept nor return any value
internally.
function reverie
N = input('Input number N: ');
if N < 10 || mod(N,10) == 0
disp('Cannot be divisible by 10. No single
digit.');
reverie;
else

disp(fliplr(num2str(N)))
end
end
2. Write a function that accepts 3 real numbers:
A, B, and C. It must display one of the three
possible outcomes (w/o quotation marks) given
the following conditions:
Outcome
"It has 2 real and distinct roots!"
-"It has 2 real and equal roots!"
-"It has 2 imaginary roots!"
*Based on the discriminant b^2-4*a*c.
function discriminant(A, B, C)
d = B^2 - 4*A*C;
if d > 0
disp('It has 2 real and distinct roots!');
elseif d == 0
disp('It has 2 real and equal roots!');
else
disp('It has 2 imaginary roots!');
end
end
3. Write a function that accepts a row vector of
real numbers, A. The function must return the
value of the maximum product of 3 consecutive
elements in A. If A has less than 3 elements, the
function must return the value 0 instead. As a
guide, the answer to A = [1 0 2 -1 2 0 4] = 0,
while B = [-2 3 4 1 0 5] = 12
function F = maxprod(A)
X=[];
if length(A) < 3
F = 0;
else
for i=1:length(A)-2
X(i)=A(i)*A(i+1)*A(i+2);
end
F = max(X);
end
end

You might also like