Introduction to MATLAB. Mark Reed Lani Clough Research Computing Group UNC-Chapel Hill. Purpose. This course is an introductory level course for beginners . The purpose of this course is to introduce you to some of the basic commands and features of MATLAB.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Introduction to MATLAB
Mark Reed
Lani Clough
Research Computing Group
UNC-Chapel Hill
Current Directory
Workspace
Current Folder
Command Window
Command History
m file comment
>> pi
ans =
3.1416
More examples:
>> sin(pi/4)
>> 2^(log(4))
>> sqrt(9)
>> x=5
x=
5
>> y = 'Bob'
y =
Bob
>> t=pi/3 VS >> t=pi/3;
>> time=61;
>> Time=61;
>> clc
>> clf
ctrl-C
…
Use Early and Often!
>> help sqrt
>> doc plot
>> lookfor regression
Mathematical Functions
>> help elfun
>> help specfun
sin(x), cos(x), tan(x), ex, ln(x)
Calculate z=e-asin(x)+10 for a=5, x=2, y=8
>> a=5; x=2; y=8;
>> z=exp(-a)*sin(x)+10*sqrt(y)
z=
28.2904
log(142), log10(142)
Matrix Generation
>> size(x)
>> A=[1 2 3; 4 5 6; 7 8 9]
>> A=
1 2 3
4 5 6
7 8 9
>>A(1,3)
>>ans =
3
A(2:3, 2:3)
A(2, :) note: just colon means all elements
A(2:end, :) note use of end keyword
The transposing operation is a single quote (’)
>>A’
Matrices can be made up of sub-matrices
This matrix consists of four 3x3 sub-matrices.
>>B= [A 10*A; -A [1 0 0; 0 1 0; 0 0 1]]Hint: note spaces to separate elements.
>>x=0:0.1:5;
>>help elmat
>>doc elmat
Data reading.
>> x = [1 3 -4];
>> y = [2 -1 7];
>> z = [3 2 3];
>> save Filename.mat
>> save Filename.mat x y
Save command
Example 2, save variables into ASCII data file
>> save Filename.dat –ascii
or
>> save Filename.txt x y –ascii
Reading and writing data files
Reading and writing data files
>> load Filename.mat
>> load Filename.mat x
>> load Filename.dat -ascii
>> dataArray = load(“myPrecious.dat”);
>> fifthColumn = dataArray(:,5);
Reading and writing data files
>> [A,B,C,...] = textread(filename, format, n);
format string specifies conversion, looks like C
n specifies number of times to repeat the format, default is to read to the end of file
Reading and writing data files
tommy 32 male 78.8
sandy 3 female 88.2
alex 27 male 44.4
saul 11 male 99.6
>> [name,age,gender,score] = textread(‘mydata.dat’, ‘%s %d %s %f’, 4);
The xlsread function
The xlsread function is to get data and text from a spreadsheet in an Excel workbook.
The basic command is:
>> d=xlsread(‘datafile.xls’)
Reading and writing data files
>>x=0:pi/100:2*pi;
>>y=sin(x);
>>plot(x,y)
>>xlabel (‘x=0:2\pi’);
>>ylabel (‘Sine of x’);
>>title (‘Plot of the Sine Function’);
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,‘--’,x,y2,‘-’,x,y3,‘:’)
>> xlabel(‘0 \leq x \leq 2\pi’)
>> ylabel(‘Cosine functions’)
>> legend(‘2*cos(x)’,‘cos(x)’,‘0.5*cos(x)’)
>> title(‘Typical example of multiple plots’)
>> x = 0:1/100:1;
>> y1 = sin(3*pi*x);
>> y2 = cos(3*pi*x);
>> y3 = sin(6*pi*x);
>> y4 = cos(6*pi*x);
>> title(‘Typical example of subplots’)
>> subplot(2,2,1), plot(x,y1)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘sin(3 \pi x)’)
>> subplot(2,2,2), plot(x,y2)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘cos(3 \pi x)’)
>> subplot(2,2,3), plot(x,y3)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘sin(6 \pi x)’)
>> subplot(2,2,4), plot(x,y4)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘cos(6 \pi x)’)
% -------------------------------------------------------
% scriptexample.m is to display soil temperature in the morning and
%the afternoon.
% -------------------------------------------------------
function[list of outputs]=functionname(list of inputs)
…….
function [F]= eff(x)
% Function to calculate values
% Input x
% Output F
for i=1:length(x)
if x(i)<0.5
F(i)=x(i)^2;
else
F(i)=0.25;
end
end
% Main program, use function: eff.m
x=-1:0.01:1;
plot(x,eff(x));
grid
xlabel('x');
ylabel('F');
title('The Piecewise Defined Function:');
m-File functions
Run the main file
Create a program!
Create a MATLAB program and function! (program shown in 2 columns)
%%MATLAB program to plot and convert soil temp
close all;
clear all;
clc;
load soilData.mat;
%assign the variables
Tc=soilData(:,1);
moist=soilData(:,2);
%create time variable
time=(1:1:24)';
%convert the soil temperature from Celsius to Fahrenheit using a function
Tf=c2f(Tc);
%plot the data
close all;
figure;
plot(time,Tf,'k',time,moist,'b.');
title('Soil Temperature and Moisture in North Carolina');
xlabel('Time (hrs of a day)');
ylabel('DegreesFarenheit, % Moisture Content');
legend('Temperature','Moisture');
%save the plots and the data
h=figure(1);
saveas(h,'soilPlot','jpg');
saveas(h,'soilPlot','fig');savetestData.matTcTf moist time;
Create a MATLAB function!
%converts celcius to farenheit
%Tc = temperature in degrees Celsius, Tf = temperature in degrees Fahrenheit
function [Tf]=c2f(Tc)
Tf = (9/5).*Tc+32;
end
Run the program!
Can be used to debug your program
Results!
x=1;
y=1;
a=z+x;
Save ‘/largefs/myoynen/test1.mat’;
Screenshot showing following is shown two slides from this slide