# Introduction to Matlab for PHYS107

In this video, I give you basic information in Matlab so you can plot your graphs in the labs. The things I showed are actually simple but you need to see some steps. The video is created in very short time interval so there might be mistakes. If you contact me, I can fix it.

Here is the example scripts.

Plotting Graph

%PHYS 107
%load phys107.txt
%x =[1 2 3 4 5 6 7];
%y =[10 35 50 75 90 105 135];
x = phys107(:,1);
y = phys107(:,2);
%x1 = smoothdata(x);
%y1 = smoothdata(y);

data = plot(x,y,’r-‘,’LineWidth’,1.01,’MarkerSize’,8);

%BESTFIT
hold on
p = polyfit(x,y,1);
pfit = polyval(p,x);
bestline = plot(x,pfit,’r-‘,’LineWidth’,2.2);

%LABELS AND TITLES
xlabel (‘Wavelength $\lambda$’,’Fontsize’,20,’Interpreter’,’Latex’);
ylabel (‘ylabel’,’Fontsize’,20,’Interpreter’,’Latex’);
text1=[‘$y =$’ num2str(p(1),4) ‘$x +$’ num2str(p(2),4)];

title (‘PHYS107′,’Fontsize’,20,’Interpreter’,’Latex’);
h= legend([data bestline],’data’,[‘bestline’ text1],’Location’,’southeast’);
set(h,’Fontsize’,15,’Interpreter’,’Latex’);

———————————————————-

Calculations

clear all
h = 6.62610-34; c = 310^8;
a= input(‘please enter “a” :’);
b= input(‘d’);
c= input(‘e’);
lambda=input(‘f’);
I = input(‘I’);
N= input(‘g’);
W12 = (Ia)/((hc)/lambda);
W21 = (Ib)/((hc)/lambda);
W13 = (Ic)/((hc)/lambda);
N3 = N/((W13)(W21)+(W13-1)(W21+W12-1))

———————————————————-

Example matrice code:

A = [0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0 ;
0 1 0 0 0 0 0 0 0 0 0 ;
0 0 1 1 0 0 0 0 0 0 0 ;
0 0 0 0 2 1 1 0 0 0 0 ;
0 0 1 0 0 1 0 3 2 1 0 ;
0 1 0 2 0 1 3 0 2 4 6 ]
S = sum(A,2); % summing each member of row with each other,
S2 = S.’; % I found the result in 9×1 matrix, I changed it to 1×9 so plotting will be easier.
S3 = flip(S2); %lowest level is 19 so changed the order (reverse data)
y= S3./11;
x= [1 2 3 4 5 6 7]; %
e = 2.718281828459046;
y2=1./(power(e,(x/2.2))-1);
data = plot(x,y,’b*’);
hold on
data2 = plot(x,y2,’r-‘);

xlabel(‘Time (s)’,’Fontsize’,20);
ylabel(‘Voltage (V)’,’Fontsize’,20);
title(‘Voltage vs Time for Part C’,’Fontsize’,20);
grid on
hold off

This site uses Akismet to reduce spam. Learn how your comment data is processed.