0% found this document useful (0 votes)
16 views4 pages

MatLab Codes - Tutorial

The document provides a comprehensive guide on various programming techniques in MATLAB, including displaying text, formatting output, defining functions, solving equations, and importing data from Excel. It covers methods for cumulative sums and products, matrix transposition, and the use of loops. Additionally, it includes examples and explanations for each method to facilitate understanding and application.

Uploaded by

debanjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

MatLab Codes - Tutorial

The document provides a comprehensive guide on various programming techniques in MATLAB, including displaying text, formatting output, defining functions, solving equations, and importing data from Excel. It covers methods for cumulative sums and products, matrix transposition, and the use of loops. Additionally, it includes examples and explanations for each method to facilitate understanding and application.

Uploaded by

debanjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Command Output

R = 5; Display this text: 5


disp(['Display this text: ', num2str(R)])
clc The value of x is 42,
clear all and the value of y is
x = 42; 3.14
y = 3.14159;
fprintf('The value of x is %d, and the value of y is
%0.2f. \n', x, y) [Note that y is now
% here %d is a placeholder for decimal values displayed with
% here the %0.2f is a placeholder for a floating point specific to only 2
value specific to 2 decimal points decimal points]
% using "fprintf" to create a professional looking Name Score
table Alice 95.50
fprintf('%-10s %10s\n', 'Name', 'Score'); % "%-10 %10" Bob 87.30
is a format specifier; "-" indicates left-alignment;
“s” is the placeholder
fprintf('%-10s %10.2f\n','Alice', 95.5);% left-aligned
text and right aligned number
fprintf('%-10s %10.2f\n','Bob', 87.3);

value = 1234.5432 Value in scientific


fprintf('Value in scientific notation is %e\n', value) notation is
%to display number in expoenntial/scientific form 1.234543e+03
Method to write a function – 1

%this function has to be saved to run successfully.


Otherwise it will show
%an unrecognized function or variable error
function f=sineplot(a,b)
t = 0:0.001:1;
f = a*sin(2*pi*b*t);
plot(t,f);
end

Method to write a function – 2

%An 'anonymous' function that does not have to be saved


f1 =@(a) a^2+5 % @ shows that "a" is the argument
f1(3) %an example run for the value 3 for a

Method to write a function – 3

%"in-line function" that also does not need to be saved


f1 = inline('a^2+5','a')
f1(3)

Solving linear equations Sol:


%’Symbolic Math Toolbox’ must be installed x: 43/14
syms x y %sysms declare x and y as symbolic variables
eq1 = 2*x+8*y ==15; y: 31/28
eq2 = 9*x-6*y ==21;
sol = solve([eq1, eq2], [x,y])

%solve first order ODE using ode45


% Problem statement: Given: x=[0,3], y(0)=1, solve the
function dy/dx = -2x^3+x-y

F = @(x,y) (-2.*x.^3+x-y);
[x,y] = ode45(F,[0 3], 1); % [0 3] is rhe ‘timespan’
and 1 is the ‘initial value’
plot(x,y)

Method to Solve simple algebraic Equation - 1 -0.5000 + 0.8660i


-0.5000 - 0.8660i
%solve the following algebraic equation: 2x^3- 2 =0 1.0000 + 0.0000i
F =[2 0 0 -2] %since x and x^2 terms are not present,
take 0 as their coefficient
roots(F)

Method to Solve simple algebraic Equation – 2 -0.5000 - 0.8660i


-0.5000 + 0.8660i
%solve the following algebraic equation: 2x^3-2 =0 1.0000 + 0.0000i
using “Symbolic Maths toolbox”
syms x
solve(2.*x.^3- 2 ==0)
double(ans)
Matrix transposition

%transpose matrix of simple numbers


a = [1 2 3]
A = a'
%transpose matric of complex number
b = [1 2-3i 3+1i]
B = b.'

Use of cumsum (Cumulative Sum) and cumprod (Cumulative


product) functions

clc
clear all
profit = [100, -50, 200, -20, 150] %Daily profits
CumulativeProfit = cumsum(profit)

%%
clc
clear all
velocity= [10, 12, 15, 14] %velocity in m/s
timestep = 1; %Time intervals in seconds
distance = cumsum(velocity*timestep)

%%
clc
clear all
growthRates =[1.05 1.03 1.04]; %Yearly growth rates
(5%, 3%, 4%)
intialInvestment = 1000; %Intial Investment
cumulativeGrowth =
intialInvestment*cumprod(growthRates)

Method to import and Excel Sheet – 1

Directly import by clicking the “Home  Import Data”. A


new Window will pop-up similar to the excel file
showing all the data. Here, one can directly select the
portion of data that needs to be imported, and then
import by clicking “Import Selected”

Note: Data imported in this format will be imported in


“Table” format, Alternatively, on this window, we can
change the “Output Type” from “table” to “Column
Vectors” and clicking “Import Selection”. If imported
this way, each column will be imported as separate
“vector”.

If there are multiple Sheets in the Excel file, go to


“View” “Top/Bottom”. This will split the pop-up window
in two and also show the option to select any
particular sheet.
Method to import and Excel Sheet - 2

Diode = xlsread('Diode_reading.xlsx', 1,'A3:B14')

%1 is referring to the particular sheet in the Excel


file that we are
%trying to import. Change the number as per requirement
%A3:B14 is representing aonly the subsection of data
that we are importing %from Sheet 1
%This method works only if the Excel sheet is the
current folder

%If the Excel Sheet is in a different folder, then we


need to give the total path before executing the above
set of commands

path(path,'C:\Users\Mrudraj\Desktop\Temp') %This can be


directly copied from the "Location" heading in the pop
up window that comes by right clicking the sheet and
checking its properties
For loops

%simple For loop


for i=0:2:10
m=(2*i)+1

end

%Nested For loop


m = 1:5
n = 1:3
for i = 1:length(m)
for j =1:length(n)
a(i,j) = m(i)+n(j)
end
end

You might also like