0% found this document useful (0 votes)
11 views

Matlab Codes

This document discusses MATLAB codes presented in lectures for grading student marks, plotting sine and cosine curves, solving initial value problems (IVPs) for ordinary differential equations (ODEs), and includes the code for a 2nd order ODE example. It provides the code and instructions for running the examples, and notes that any functions used must be saved separately.

Uploaded by

Udita Goswami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Matlab Codes

This document discusses MATLAB codes presented in lectures for grading student marks, plotting sine and cosine curves, solving initial value problems (IVPs) for ordinary differential equations (ODEs), and includes the code for a 2nd order ODE example. It provides the code and instructions for running the examples, and notes that any functions used must be saved separately.

Uploaded by

Udita Goswami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MATLAB Codes Discussed in Lectures

(If you find difficulty in understanding the following codes, contact me on my email id)

%% GRading code
Marks = [40, 59, 70, 89, 94]; % Marks as
input
No_of_St = length(Marks); % No. of
students
Grade = zeros(1,No_of_St); %
Initialization of Grade array
for i = 1:No_of_St
if Marks(i)>=90
Grade(i) = 10;
elseif Marks(i) >=80
Grade(i) = 9;
else
Grade(i) = 5;
end
end

Note: Turn off comments(%) to run code


%% Matlab plotting
close all
clear all
x = 0:pi/100:2*pi;
y = sin(x);
z = cos(x);
%
% figure % sine figure
% plot(x,y)
%
% xlabel('X-value')
% ylabel('sin(X)')
% title('Sine curve')
% legend('X axis values')
%
% %
% figure % cosine figure
% plot(x,z)
%
% xlabel('X-value')
% ylabel('cos(X)')
% title('Cosine curve')
% legend('X axis values')
%
% %
% figure
% plot(x,y,x,z)
%
% xlabel('X-value')
% ylabel('Y')
% title('Sine & Cosine curve')
% legend('Sine cureve', 'Cosine
curve')
%
% figure
% plot(x,y,'go-
','linewidth',4,'markersize',10
)
% hold on
%
plot(x,z,'d','markersize',10,'C
olor',[0.37,0.6,0.3])
%
% xlabel('X-value')
% ylabel('Y')
% title('Sine & Cosine curve')
% legend('Sine cureve', 'Cosine
curve')
%
% %
% r = 2;
% xc = 4;
% yc = 3;
%
% theta = linspace(0,2*pi);
% x = r*cos(theta) + xc;
% y = r*sin(theta) + yc;
% figure
% plot(x,y)
% axis equal
%
% x = linspace(0,3*pi,200);
% y = cos(x) + rand(1,200);
% sz = linspace(1,100,200);
% %c = 5*ones(1,length(x));
% c=linspace(1,10,length(x));
% figure
% scatter(x,y,sz,c)
% figure
% plot(x,y)
%
X = randn(1000,1);
Y = randn(1000,1);
h = histogram(X)
nbins = 10;
figure
h2 = histogram(X,nbins)
% %% IVP solver - 1 ODE
% odefun = @(t,y) 2*t;
% tspan = [0 10];
% y0 = 1;
%
% options = odeset('RelTol',1e-
6);
%
% [t,y] =
ode45(odefun,tspan,y0,options);
%
% %plotting
% plot(t,y,'o','linewidth',2)
% hold on
% plot(t,(t.^2+1))
% legend('ODE45','Analytical')
% xlabel('t')
% ylabel('y')

% %% IVP solver - Multiple ODE


% tspan = [0 10];
% y0 = [1;0];
%
% options = odeset('RelTol',1e-
6);
%
% [t,y] =
ode45(@fun_name,tspan,y0,option
s);
%
% %plotting
% plot(t,y(:,1),'o-
','linewidth',2)
% hold on
% plot(t,y(:,2),'*--
','linewidth',2)
%
%
% %legend('ODE45','Analytical')
% xlabel('t')
% ylabel('y')
%% 2nd order ODE
mu = 1;
tspan = 0:0.1:10;
y0 = [1; 0];
options = odeset('RelTol',1e-
3);
[t,y] =
ode45(@odefun3,tspan,y0,options
,mu);
plot(t,y(:,1),'o-
','linewidth',2)
hold on
plot(t,y(:,2),'*--
','linewidth',2)
legend('y','dy/dt')

Note: The used functions have to be saved


as individual files in the same working folder.
The used functions are defined below:

function dy = fun_name(t,y)
len = length(y);
dy = zeros(len,1);
dy(1) = 2*y(1) + y(2);
dy(2) = 3*y(1) -5*y(2);
end

function dy = odefun2(a,b,m)
dy = zeros(2,1);
dy(1) = b(2);
dy(2) = m*(1- b(1).^2 ).*b(2) -
b(1);
end

You might also like