computer programming in matlab - atauni
TRANSCRIPT
Atatürk University
Functions, IO
Atatürk University
Prof. Dr. İrfan KAYMAZ
Computer Programming in
MATLAB
Atatürk University Engineering Faculty
Department of Mechanical Engineering
Atatürk University
Functions, IO User Defined Functions
MATLAB has a number of built-in (compiled) functions, e.g. sin , sqrt, sum. If you find that you are often building a function from several Matlab commands, you can develop a user-defined function that can be used in a same way as the built-in Matlab functions.
Atatürk University
Functions, IO User Defined Functions
Syntax
function [output variables] = function_name(input variables);
defines function function_name that accepts inputs variables and returns outputs variables.
The rules for writing an M-file (user defined) function are the following:
1- The first line of a function has to be the following syntax:
function [output variables] = function_name(input variables);
2- Function written to an m-file must be saved with the same name of «function_name»
3- A user-defined function is called by the name of the m-file.
Atatürk University
Functions, IO Example 1
Write a function that computes the final velocity of a free-falling object.
Function file (file name sonhiz.m) (subprogram) function v= sonhiz(g,h) % file must be named with sonhiz v=sqrt(2*g*h); m_file (Main program) clc clear all g=9.81; h=10; v=sonhiz(g,h)
Atatürk University
Functions, IO Example 2
Write a function that computes the the area and perimeter of a cicrcle.
Function file (file name circle.m) (subprogram) function [area,perimeter]=circle(R) area=pi*R^2; perimeter=2*pi*R; M_file (Main program) clc clear all R=1; [area,perimeter]=circle(R)
Atatürk University
Functions, IO Example 3
Write a function that computes the distance between two points
x1= x coordinate of the first point; x2= x coordinate of the second point
y1= y coordinate of the first point; y2= y coordinate of the first point
Function file (dis.m) (subprogram) function distance= dis(x1,y1,x2,y2) distance=sqrt((x2-x1).^2+(y2-y1).^2); m_file (Main Program) clc clear all x1=3; y1=4; x2=1; y2=2; distance= dis(x1,y1,x2,y2); fprintf('distance between two points=%g\n',distance);
Atatürk University
Functions, IO Example 4
Write a MATLAB program that computes the value of the following equation according to the x and y received from keyboard.
F(x,y)=x2×y+x.y+ln(x)+1
log10 (y)+tan(
x
y)
Atatürk University
Functions, IO Example 5
Write a MATLAB program to solve the value of the following equation for x from 3 to 13 by increments of 0.1, and then plots the x and y data in the main m-file. The values of the variables must be given in the main m-file, a=5, b=6, and c=8
Atatürk University
Functions, IO Solution 5
Main m-file (Named with: soru.m )
clc
clear all
x=3:0.1:13
a=5;b=6;c=8;
y=deneme(a,b,c,x);
plot(x,y)
Function File (Named with deneme.m)
function y=deneme(a,b,c,x);
y=a+(b*x.^2)/2+(c*exp(x)+2)/3;
Atatürk University
Functions, IO Example 6
Write a Matlab program to find the sum and mean of the successive odd integers from 1 to 35, and print the results in the following format.
Sum of numbers =324
Mean of numbers =18
Atatürk University
Functions, IO IO
When you get into serious programming, you will often need to store data on a disk. The process of moving data between MATLAB and disk files is called importing (from disk files) and exporting (to disk files).
Direct input of data from keyboard becomes impractical when the amount of data is large and the same data is analyzed repeatedly.
In these situations input and output is preferably accomplished via data files.
The process of moving data between MATLAB and disk files is called importing (from disk files) and exporting (to disk files).
Atatürk University
Functions, IO File open: fopen
fopen, opens a file or obtain information about open files.
Syntax
fid = fopen(‘filename’, ‘permission’)
opens the file filename in the mode specified by permission and returns fid, the file identifier.
fid-> file identifier
filename -> file name
permission -> r,w,.., etc.
Atatürk University
Functions, IO File open: fopen
'r' Open file for reading (default).
'w' Open or create new file for writing. Discard existing contents, if any.
'a' Open or create new file for writing. Append data to the end of the file.
'r+' Open file for reading and writing.
'w+' Open or create new file for reading and writing. Discard existing contents, if any.
'a+' Open or create new file for reading and writing. Append data to the end of the file.
Atatürk University
Functions, IO File close: fclose
fclose closes one or all open files.
Syntax
status = fclose(fid);
fid-> file identifier
Status-> if succesful 0; otherwise -1.
Atatürk University
Functions, IO Writing data to a file: fprintf
Write formatted data to file.
Syntax:
fprintf(fid,format,A,...)
fprintf (fid,‘%format%format%format….’,var_1,var_2,var_3,….)
Atatürk University
Functions, IO Example 7
Write a MATLAB program that writes the following M array to a file named with «veri.dat»
M=[1 2 3 4];
a=fopen('veri.dat','w');
fprintf (a,'%d %d %d %d',M);
fclose(a);
M=[1 2 3 4];
a=fopen('veri.dat','w');
for i=1:4
fprintf(a,'%d ',M(i));
end
fclose(a);
With for loop
Atatürk University
Functions, IO Example 8
Write a MATLAB program that writes the following M matrix to a
file named with «veri.dat»
A=[1 5 11 ; 2 4 5];
a=fopen('veri.dat','w+');
for i=1:2
for j=1:3
fprintf(a,'%d ',A(i,j));
end
end
fclose(a);
Atatürk University
Functions, IO Reading data from a file: fscanf
fscanf: fscanf reads formatted data from a file
Syntax
[value, number_of_values_read] = fscanf(file_variable, 'format', count);
value : The returned information is the value
number_of_values_read : the number of values actually read count : Amount of data to read: n, [n, m], Inf
Atatürk University
Functions, IO Example 9
Write a MATLAB program that reads the all data from a file named with «veri.dat» and assigns them to b variable.
veri.dat 1 5 11 2 4 5
clear all
clc
a=fopen ('veri.dat', 'r' );
[b,sayi]=fscanf(a,'%d ',inf);
fclose(a);
b
Atatürk University
Functions, IO Example 10
Write a MATLAB program that reads the first two data from a file named with «veri.dat» and assigns them to b variable.
veri.dat 1 5 11 2 4 5
clear all
clc
a=fopen ('veri.dat', 'r' );
for i=1:2
b(i)=fscanf(a,'%d ',[1]);
end
fclose(a);
b
Atatürk University
Functions, IO Reading data from a file to more than one variable
To read the data from the file which is in the same format
with the left side;
a = fopen('veri.dat', 'r');
b = fscanf(a, '%g %g', [2 inf]);
fclose(a)
Veri.dat
1 10
3 12
4 14
5 15
6 16
7 18
1 3 4 5 6 7
10 12 14 15 16 18
To assign the rows to different variables
c=b(1,:)
d=b(2,:) a=fopen('veri.dat','r')
for i=1:6
d(i)=fscanf(a,'%f',[1]);
e(i)=fscanf(a,'%f',[1]);
end
fclose(a)
To read the data direcly
from the file
RESULT is
Atatürk University
Functions, IO
Another method;
clc
clear all
a=fopen('veri.dat','r');
for i=1:6
for j=1:2
masa(i,j)=fscanf(a,'%g',[1,1]);
end
end
fclose(a);
k=(masa(:,1))' % tırnak işareti transpoz işlemidir
t=(masa(:,2))'
Veri.dat
1 10
3 12
4 14
5 15
6 16
7 18
Reading data from a file to more than one variable
Atatürk University
Functions, IO Example 11
In a cargo company, a pricing policy according to the package weight is as follows; Base price is 5 TL for packages up to 2 kg. For the packages heavier than 2 kg, 0.5 TL should be added to the base price for every 1 kg. If the packages is heavier than 35 kg, 10 TL should be added to the total price. The packages heavier than 50 kg are not accepted because of worker’s health. Write a Matlab program that computes the price according to the above-mentioned scheme and then writes the following table for 1:50 kg pacages to a file named with veri.dat. (Assume that the scale readings are only integer values)
Ağırlık (kg) Fiyat (TL)
1 5
2 5
3 5.5
4 6
… …
50 39