11
Eki
2024

Matlab ile uygulama geliştirme dersinde yazılan kodlar

11.10.2024

Sayının karesini döndüren fonksiyon

function [sonuc] = kare(x)
    sonuc = x*x;
end

Dik üçgenin hipotenüs, çevre ve alanını döndüren fonksiyon

function[hipo,cevre,alan] = hesaplaucgen(a,b)
hipo = sqrt(kare(a)+kare(b))
cevre = a + b + hipo;
alan = (a*b)/2;
end

a ve b kenarı girilen dik üçgenin hipotenüsünü ve iç açısını döndüren fonksiyon

clc, clear;
a = input('a kenarını giriniz:');
b = input('b kenarını giriniz:');
c = sqrt(kare(a)+kare(b));
disp(['hioptenüs:'        num2str(c)]);
x = asind(b/c);
disp(['x açısı:'     ,    num2str(x)]);

25.10.2024

if-else yapısı

clc;
a = input('bir sayı gir:');
if a>0
    disp('a pozitif sayı');
elseif a<0
    disp('a negatif sayı');
else 
    disp('a sıfır');
end

switch-case yapısı

gun = input('haftanın kaçıncı günü lazım?');

switch gun
    case 1
        disp('Pazartesi')
    case 2
        disp('Salı')
    case 3 
        disp('Çarşamba')
    case 4
        disp('Perşembe')
    case 5 
        disp('Cuma')
    case 6
        disp('Cumartesi')
    case 7
        disp('Pazar')
    otherwise
        disp('Yanlış giriş yaptın')
end

try-catch yapısı

%Try catch örneği
a = 2;b = 3;c = 4;
try
    disp(a*b*c*k) % Burası hatalı olduğu için atla
catch
    disp(a*b*c) % Burayı çalıştır
end

1.11.2024

error() komutu kullanımı

format compact
a = 1e4;
while 1
    disp('Sinir bozucu döngü');
    a = a-1;
    if a==5000 
        error('Dikkat a 5000 oldu');     
    end
end

x 1…20 iken y=x2 grafiği

x = 1:20; y= power(x,2)
plot(x,y)
title('y=x^2 grafiği')
xlabel('x ekseni')
ylabel('burası da y ekseni')

Dairenin yarıçapı-alanı grafiği

r = 0:10;
a = pi*r.^2;
plot(r,a);
xlabel('yarıçap');
ylabel('alan');
title('yarıçap-alan grafiği');

Polinom grafiği

x = -100:10;
y = [1 0 -6 0 0 0 5 0 -10 7];
fx = polyval(y,x);
plot(x,fx);
xlabel('x değeri');
ylabel('f(x)');
title('Örnek soru');
grid off;
box off;

8.11.2024

e-xsinx grafiği

x = 0:0.01:5;
y = exp(-x).*sind(x);
plot(x,y,'linewidth',2);

subplot kullanımı

x = 0:360;
fx = sind(x);
gx = cosd(x);
yx = x.^2;
subplot(2,2,1); plot(x,fx);
subplot(2,2,2); plot(x,gx);
subplot(2,2,3); plot(x,yx);
clear,clc;
x = 0:360;
fx = exp(sind(2*x));
gx = exp(cosd(2*x));
yx = exp(tand(2*x));
subplot(1,3,1); 
plot(x,fx,'r');
title('sinüs','fontsize',15);

subplot(1,3,2); 
plot(x,gx,'k');
title('kosinüs');

subplot(1,3,3); 
plot(x,yx, 'm');
title('tanjant');