function[A,V]=rumus_bola(r)
% A adalah luas permukaan bola = 4*pi*r^2
% V adalah volume bola = 4/3*pi*r^3
% Operasi yang dijalankan
A=4*pi*r^2;
V=4/3*pi*r^3;
Pada command window
>> [A,V]=rumus_bola(4)
A =
201.0619
V =
268.0826
Contoh 2
Menampilkan grafik persamaan parabola dar
i fungsi parabola.m
Membuat fungsi parabola dalam M-file
function[y]=parabola(x)
y=x.^2; % Persamaan parabola
y =
Columns 1 through 20
100 81 64 49 36 25 16 9 4 1 0 1 4
9 16 25 36 49 64 81
Column 21
100
>> plot(x,y)
>> xlabel('sumbu x')
>> ylabel('sumbu y')
>> title('Grafik Persamaan Parabola')
Grafik Persamaan Parabola
Latihan
Buatlah fungsi perhitungan kecepatan (v)
dan jarak tempuh (x) pada GLBB.
Variabel t (waktu), a (percepatan), dan vo
(kecepatan awal)
Fungsi GLBB.m
function[v,x]=glbb(t,a,vo)
% Perumusan kecepatan dan perpindahan GLBB
% Perhitungan kecepatan
v=vo+a.*t;
% Perhitungan perpindahan
x=vo.*t+1/2*a*t.^2;
Memanggil fungsi GLBB.m
>> t=0:10;
>> a=2;
>> vo=0;
>> [v,x]=glbb(t,a,vo)
v =
0 2 4 6 8 10 12 14 16 18 20
x =
0 1 4 9 16 25 36 49 64 81 100
Example of Matlab Applicatio
n
function [hmax,dmax]=trajectory(v0,theta) thmax=v0y/g;
% trajectory calculates the max height and di hmax=v0y^2/(2*g);
stance of a ttot=2*thmax;
projectile, and makes a plot of the trajector dmax=v0x*ttot;
y.
% Creating a trajectory plot
% Input arguments are:
tplot=linspace(0,ttot,200);
% v0: initial velocity in (m/s).
x=v0x*tplot;
% theta: angle in degrees.
y=v0y*tplot-0.5*g*tplot.^2;
% Output arguments are:
plot(x,y)
% hmax: maximum height in (m).
xlabel('DISTANCE (m)')
% dmax: maximum distance in (m).
ylabel('HEIGHT (m)')
% The function creates also a plot of the tra
jectory.
title('PROJECTILE''S
TRAJECTORY')
g=9.81;
v0x=v0*cos(theta*pi/180);
v0y=v0*sin(theta*pi/180);
Panggil fungsi di command window [h d]=trajectory(230,39)