Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

MATLABEx1B_Q3.1_816009317.m

%Scalar Variable creation
 
% i)assigns the value 10 to a
a =10
 
% ii)b is assigned 2.5*10^23
b =2.5*10^23
 
% iii) c is assigned the complex number 2+3i
c=2+3i
 
% iv)d is assigned the value e^((2*pi/3)j)
d= exp(2*pi/3 *j)

Matlab Anonymous Functions

%Scalar Variable creation
 
% i)assigns the value 10 to a
a =10
 
% ii)b is assigned 2.5*10^23
b =2.5*10^23
 
% iii) c is assigned the complex number 2+3i
c=2+3i
 
% iv)d is assigned the value e^((2*pi/3)j)
d= exp(2*pi/3 *j)

functions




fprintf('######### Anonymous Functions  ################\n')

power = @(x, n) x.^n;

result1 = power(7, 3)
result2 = power(49, 0.5)
result3 = power(10, -10)
result4 = power (4.5, 1.5)



fprintf('######### Functions  ################\n')

function avg = average(nums)
global TOTAL
avg = sum(nums)/TOTAL;
end

TOTAL = 10;
n = [34, 45, 25, 45, 33, 19, 40, 34, 38, 42];
av = average(n)




formating strings

str = 'The range for int8 is:\n\t %d to %d ';
 % the \t used the print something like :
 %             output
 % instead of:
 % output
 
 
sprintf(str, intmin('int8'), intmax('int8'));
 % Whave we have done here is : printing a string then  go to the next line (\n) then add some tabs (\t) then print the two nubers (%d a_string %d)
 
 
 
 
 % here are two ways creat a 2 dim  array with strings
doc_profile = ['Yasmine Najmi                             '; ...
               'Sr. Surgeon                          '; ...
               'RN Tagore Cardiology Research Center'];
               
doc_profile = char('Yasmine Najmi', 'Sr. Surgeon', ...
                  'RN Tagore Cardiology Research Center');
                  



%FORMATTING STRINGS

A = pi*1000*ones(1,5)
% N = (3111.6   3222.6   3333.6   3444.6   3555.6) ;
sprintf(' %f \n %.2f \n %+.2f \n %11.4f \n %013.2f \n', A)                
                  
                
                  
% the term  % x.yf  applied to the number 
% 3141.592654 means  print this number with y digit after the dot 
% lets say the result is a float with n digits 
%  x.yf  print this new nuber and adds n-x tabs befor it 


sprintf("###########################")


students = {'Zara Ali', 'Neha Bhatnagar', ...
            'Monica Malik', 'Madhu Gautam', ...
            'Madhu Sharma', 'Bhawna Sharma',...
            'Nuha Ali', 'Reveva Dutta', ...
            'Sunaina Ali', 'Sofia Kabir'};
 
% The strrep function searches and replaces sub-string.
%this one replace evry 'v' with 'n'
corrected_name = strrep(students(8), 'v', 'n')
students(8) = corrected_name

% Display first names
first_names = strtok(students)

Execute MATLAB/Octave Online

x=linspace(0,5);
y=sin(x);
subplot(3,1,1)
plot(y)
xlabel "x1"
ylabel "y1"


z=cos(x);
subplot(3,1,2)
plot(z)
xlabel "x2"
ylabel "y2"

t=conv(y,z);
subplot(3,1,3)
plot(t)
xlabel "x3"
ylabel "y3"

data types

polyder([4 5 6 7 8 0 ]); 

A = [1 1 1 ; 2 2 2 ; 3 3 3 ] ;
B = [1 1 1 ; 2 2 2 ; 3 3 3 ] ;
D = [1 1 1 1 ; 2 2 2 2 ; 3 3 3 3] ;



% COMMENT 

numel(A);
size(A);
det(A);
inv(A);

x = 3 ; 
y = 4 ; 

x+y ;

A*B;
A.*B;

A(3,2);

C = repmat(A, 3, 4);

C([2, 6], :) ;
C([2, 6], [3, 7]);
C([2:4:end], [3, 7]);

2:3:100;

ans; % calls most recent answer

sqrt(136);
163 / ans;

% multiple asignments 

a = 2; b = 7; c = a * b ;

%  the command     who      tells you all the variables you have used


% the command      whos     tells you more about the varibles (their size, Bytes, Class)


initial_velocity = 0;
acceleration = 9.8;
time = 20;
final_velocity = initial_velocity + acceleration * time;


% the format command 

pi;
x = 7 + 10/3 + 5 ^ 1.22;

format long;   %16 digits after decimal
x = 7 + 10/3 + 5 ^ 1.22;
pi;

format short  % the default format 4 places 
x = 7 + 10/3 + 5 ^ 1.22;
pi;

%The format bank command rounds numbers to two decimal places
format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6 ;

format short e  % the e in the output is 10
4.678 * 4.9;

% you can also use the format long e 


%The format rat command gives the closest rational expression resulting from a calculation
format rat
4.678 * 4.9;






































Execute MATLAB/Octave Online

x=[1 2 3 4 5 6 7 8 9 10];
%x=[0 45 90 135 180 225 270 315 360 405 450 495 540 585 630 675 720];

%y=sin(x*pi/180);

for i=1:10
    if (rem(i,3)==0)
    y(i)=1

    else
     if (rem(i,3)==0)
    y(i)=0
    
    else
    i=i+3;
end
    end

end
scatter(x,y);
line(x,y);
title('rectangular curve');
xlabel('X Axis');
ylabel('Y Axis');


print -dpng figure.png

triangular wave

x=[1 2 3 4 5 6 7 8 9 10]
%x=[0 45 90 135 180 225 270 315 360 405 450 495 540 585 630 675 720];
%y=sin(x*pi/180);
for i=1:10
    if (rem(i,2)==0)
    y(i)=1

    else
    y(i)=0

    end

end
scatter(x,y);
line(x,y);
title('plot title');
xlabel('X Axis');
ylabel('Y Axis');


print -dpng figure.png

triangular curve

x=[1 2 3 4 5 6 7 8 9 10]
%x=[0 45 90 135 180 225 270 315 360 405 450 495 540 585 630 675 720];
%y=sin(x*pi/180)
for i=1:10
if (rem(i,2)==0)
y(i)=1
else
y(i)=0
end
end
scatter(x,y)
line(x,y)
title('plot title')
xlabel('x Axis')
ylabel('y Axis')
print -dpng figure.png

Execute MATLAB/Octave Online

a = int32(7)

b = a
a = a^4
print -dpng



Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.