Skip to main content

control matlab practice




1. There are some built in constants in MATLAB; i, j, pi, eps, inf, NaN. Whether a name is a built in constant or not can be checked by the statement ''which -all <name>''.

2. Type the following vector in the command window:
a) 𝐴= 123 b) 𝐵= 01−2−3 c) 𝐶= −120410−2106
i) Use MATLAB to find the value in the second row and the third column of matrix C.
ii) Use MATLAB to find the second row of matrix C.
iii) Use MATLAB to find the third column of matrix C.
iv) Use MATLAB to delete the second column of matrix C.

3. You can also perform Element-wise assignment of the Matrix elements.
a) >> A(1,1)=1;  b) >> A_r1=[1 2];                     c) >> e_col1=[1;3.7;8];
>> A(2,1)=3.7;        >> A_r2=[3.7 -0.002];             >> e_col2=[2;2e-3;1.3e5];
>> A(1,2)=2;          >> A_r3=[8 13000];                 >> e_col3=[5;1.3e7;7];
>> A(3,2)=1.3e5;     >> A=[A_r1; A_r2; A_r3] ;      >> E=[e_col1 e_col2 e_col3];

4. Construct two matrices on your own; A and B and use the following operators:
a) A+B b) A-B c)A * B d) A.*B e) A. /B f)A. ^B
Also, Try some useful functions for Linear Algebra in MATLAB;
a) rank b) det c)inv d) eig e) ones f) eye g) diag h) transpose(use A')
Note: You can type ''help matfun'' (Matrix functions-numerical linear Algebra) in the command window for more information, or type ''elmat'' (Elementary matrices and matrix manipulation).



6. You can try some functions for creating arrays in MATLAB;

7. Plotting; Here are some useful functions for creating plots.


Example:
>> x=0:pi:2*pi;                >> x=0:pi/100:2*pi;
>> y=sin(x);                     >> plot(x,sin(x))
>> y1=sin(x-.25);            >> hold on
>> y3=sin(x-.5);              >> plot(x, cos(x))
>> plot(x,y,x,y1,x,y3)     >> hold off

Q. Given the autonomous system, 𝑥 =𝑎𝑥
where, 𝑎=−1𝑇, where T is the time constant. The solution for the differential equation is,
𝑥 𝑡 =𝑒𝑎𝑡𝑥𝑜 . Set T=5 and the initial condition x(0)=1.
a) create a script in MATLAB(.m file) where you plot the solution x(t) in the time interval 0 ≤t ≤25.
b) Add grid, and proper title and axis labels to the plot.
c) check out the help for the following 2D functions in MATLAB; semilogx, bar, hist, pie,scatter.


8. Create a vector with random numbers between 0 and 100. Find the following statistics; mean, median, standard deviation(std(x)) , minimum(min(x)), maximum(max(x)) and the variance(var(x)).

9. Given two complex numbers: 𝑐=4+𝑗3 𝑑=1−𝑗
a) Find the real and imaginary part of c and d in MATLAB. Also, find c+d and c-d, c*d and c/d.
b)Check specific functions abs, angle, imag, real, conj and complex.
c) Find r and θ. Find also the complex conjugate.

10. MATLAB offers lots of functions on polynomials, such as conv, roots, deconv, polyval, polyint, polyfit,etc . Look up these functions in the Help system in MATLAB.

Q. Define the following polynomial in MATLAB: 𝑝 𝑥 =−2.1𝑥4+2𝑥3+5𝑥+11
a) Find the roots of the polynomial.
b) Find p(x=2).

11. Given the following polynomials: 𝑝1 𝑥 =1+𝑥−𝑥2 𝑝2 𝑥 =2+𝑥3
a) Find the polynomial 𝑝 𝑥 =𝑝1 𝑥 .𝑝2 𝑥
b) Find the roots of the polynomial (p(x)=0) and find p(x=2).
c) Find the differentiation of 𝑝2(𝑥)

12. Find the sixth order polynomial that best fits the following function. 𝑦=sin⁡(𝑥)
Also, plot both the function and the sixth order polynomial to compare the results.

13. Create the following expression in MATLAB. 𝑓 𝑥 =log 𝑎𝑥2+𝑏𝑥+𝑐 −sin⁡(𝑎𝑥2+𝑏𝑥+𝑐)4𝜋𝑥2+cos 𝑥−2 (𝑎𝑥2+𝑏𝑥+𝑐)
Given, a=1, b=3,c=5, Find f(9) , [ Tip! You can split the expressions into different parts. This makes the expression simpler to read and understand, and you minimize the risk of making an error while typing the expression in MATLAB.]

14. Find the solution for the given equation. 𝑥1+2𝑥2=5
3𝑥1+4𝑥2=6 7𝑥1+8𝑥2=9
You need to set the equations on the following form,
a) 𝐴𝑥=𝑏 and calculate x=inv(A)*b.
b) Try using x=A\ b.

15. You can use the functions ''tic'' and ''toc'' to find the execution time. Try this simple program that calculates 𝑦=cos 𝑡 𝑓𝑜𝑟 𝑡=1 𝑡𝑜 100000
clear
tic
t_max=100000;
y=zeros(t_max,1);
for t=1:t_max
y(t,1)=cos(t);
end
toc















Comments

Popular posts from this blog

2017//2(b)//Engineering Eco//KU

A loan of $10,000 is to be repaid over a period of eight years. During the first four years, exactly half of the loan principal is to be repaid (along with accumulated compound interest) by a uniform series of payments of A1 dollar per year. The other half of the loan principal is to be repaid over four years, with accumulated interest, by a uniform series of payments of A2 dollar per year, If i=9% per year, what are A1 and A2?

Testing servo motor with arduino

  Testing servo motor with arduino  Components Needed: Arduino board (e.g., Arduino Uno) Servo motor Jumper wires Steps for Connection: Servo Motor Pins: Ground (GND): Usually the brown or black wire of the servo motor. Power (VCC): Usually the red wire of the servo motor. Control Signal (PWM): Usually the yellow, orange, or white wire of the servo motor. Connecting to Arduino: Ground (GND): Connect the ground wire of the servo motor to one of the GND pins on the Arduino. Power (VCC): Connect the power wire of the servo motor to the 5V pin on the Arduino. Control Signal (PWM): Connect the control signal wire of the servo motor to digital pin 9 on the Arduino, as specified in the code by myservo.attach(9); Connection Arduino  Servo motor  VCC/ 5V Red wire  GND Black Wire  9  Yellow wire Explanation: Ground (GND): This connection ensures that the servo motor and the Arduino share a common ground, which is necessary for proper operation. Power (VCC): The servo motor needs a power supply t

CanSat

  CanSat is a type of small satellite that is designed to fit inside a soda can. These miniature satellites are used for a variety of purposes, including educational projects, scientific research, and commercial applications. The CanSat concept was first developed in 1998 by Bob Twiggs, a professor at Stanford University, and Jordi Puig-Suari, a professor at California Polytechnic State University. They wanted to create a low-cost, hands-on way for students to learn about satellite technology and space science. Since then, CanSats have become a popular platform for educational projects around the world. They are often used in STEM (Science, Technology, Engineering, and Mathematics) education programs, where students are tasked with designing, building, and launching their own CanSats. A typical CanSat consists of a soda can-sized container that houses a variety of sensors, such as temperature, humidity, pressure, and acceleration sensors. It also includes a small computer, a radio tran