Showing posts with label Matlab tutorial. Show all posts
Showing posts with label Matlab tutorial. Show all posts

Monday, November 23, 2020

, ,

fmincon Matlab Function With fmincon Example

fmincon function in MATLAB is used to find the minimum of any given nonlinear multivariable function.

MATLAB Syntax : - 

 

MATLAB Syntax
MATLAB Syntax

Description For fmincon MATLAB

 Nonlinear programming solver.

Let see the a problem here and find the minimum value for this problem.

fmincon MATLAB
fmincon MATLAB

 Well b and beq are vectors, A and Aeq are matrices in this given equation on the other hand c(x) and ceq(x) are the functions in equation that return vectors.

well as you can see in the equation f(x) is a function which returns a scalar and remember all the three function f(x),ceq(x), and c(x) are non linear.

lb,x and ub can be passed as vectors or matrices in the fmincon MATLAB.

  •  x = fmincon(fun,xθ,A,b) function is started with the xθ (x theta) and try to find the minimizer x for the given function and the expression for this  A*x ≤ b. xθ can be a scalar, vector, or matrix.
  •  x = fmincon(fun,x0,A,b,Aeq,beq) minimize this function to linear equalities Aeq*x = beq and A*x ≤ b.If there is no inequalities exist, then set A = [] and b = [].
  • x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) helps to define the lower and the upper case on the design variables in x, that's why the solution always exixt in between lb ≤ x ≤ ub.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) will helps to minimization to the nonlinear inequalities c(x) or the equalities ceq(x) defined in nonlcon.
 
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) function helps to minimizes with the optimization options which is specified in options.

x = fmincon(problem) helps to finds the minimum for the given problem, and a structure described in problem.
 
[x,fval,exitflag,output] = fmincon(___) will helps to return a value exitflag that describes the exit condition of fmincon function MATLAB.


Example for fmincon function MATLAB

Find the minimum value of Rosenbrock's function if there is a linear inequality constraint. 

 

 

Example for fmincon
Example for fmincon function MATLAB


 

Saturday, November 21, 2020

, , ,

sprintf Function In MATLAB With Example Program

 stg = sprintf(formatSpec,B1,...,Bn) use to formats the data in an array with the help of  formatting operators which is specified by formatSpec in MATLAB and returns the resulting text in a define variable(stg) then stg known as output and otherwise stg is an character.

MATLAB Program for Literal Text and Array Inputs

formatSpec = 'The array is %dx%d.';
A1 = 2;
A2 = 3;
str = sprintf(formatSpec,A1,A2)

Sprintf Program example for MATLAB
Program example for MATLAB

 

compose function is used to return the number of pieces of formatted text as a string array and that can be used with the MATLAB sprintf() Function.

str = sprintf(literalText) In MATLAB use to translates escape-character sequences of the literalText which is define with sprintf function in MATLAB for example :-  \n and \t and sprint function return the all other  characters unaltered.

Now if the string contain any  formatting operator (example %f) then sprint function in MATLAB discards it and all characters after that operator. 

Input Arguments for sprintf function in MATLAB

  • The Output is format with the help of  formatSpec and this also include the ordinary text and special characters.
  • if  formatSpec is include the escape characters in string then  sprintf Function translates that escape characters (such as /n).

Formatting Operator of sprintf

A formatting operator for sprintf starts with a percent sign, %, and ends with a conversion character and the conversion character is also required.

You can also specified some identifier like subtype operators, field width, precision, and  flags between % and  conversion character which is an end point.

Read more  : -  fmincon Matlab Function

List Of Conversion Character for Sprintf function in MATLAB

There some of the Conversion Character

Conversion Character for Sprintf function in MATLAB
Conversion Character for Sprintf function


Some tips : - 

    The sprintf function in MATLAB is similar to fprintf function but the fprintf function is used for the Command Window.


 

Thursday, November 19, 2020

, ,

fprintf command And fprintf format in MATLAB

  •  The fprintf command in MATLAB Programming use to displays formatted text which is centered on the icon and this fprint function can display formatSpec with a contents of var.
  • formatSpec  in MATLAB can be a character vector with the single quotes, or a string scalar.

Formatting for the fprint function in MATLAB : -

  •  formatting is starts with the a percentage sign, % and it will end with the conversion character.
  • Remember conversion character is required in formatting Operator.
  • But you can use the  flags, field , identifier,width, and subtype,precision operators between % and conversion character.

 List for the Conversion Character

Conversion Character
Conversion Character in MATLAB

Example of fprint function in MATLAB

The command

fprintf('YES YES');

displays this text 'YES YES' on the icon.

The follow command

fprintf('YES YES = %d',16);

uses the decimal notation format (%d) to display the variable 16.


Using fprintf in MATLAB With Function



Let's see this example for define a function in MATLAB : -
function []= fun2let(n)
 if n > 90.00
 fprintf('>>letter grade to %d is:A+\n',n)
 elseif n<=89.49 && n>=80.00
 fprintf('>>letter grade to %d is:B+\n',n)
 % YOU COMPLETE YOUR FUNCTION WITH OTHER TESTS
 elseif n<59.5 
 %    grade='Fail'
    fprintf('>> letter grade to %d is:Fail\n',n)
 end
 end

Wednesday, November 18, 2020

, , ,

fft matlab Function And ftt code with Example

 Fast Fourier transform of fft function in matlab.

Syntax:-

Y = fft(X)
Y = fft(X,n)
Y = fft(X,n,dim)

Discprition for fft matlab Function

If x is an vector then it will return the Fourier transform of that vector

Let x is an matrix then fft(X) assume the colum as a vactory and return Fourier transform of each column.

Now let assume X is an multidimensional array then fft(X) treats the values of first array dimension only if the size of that array is not equal to 1 as vector and gives the Fourier transform of each vector

Cases with the fft() MATLAB Function

Y = fft(X,n) returns the n-point DFT.

Case 1

X is an vector and the length of X is lower then n. X will be padded with trailing zeros to length n

Case 2

X is an vector and the length of X is Higher then n. X will be truncated to length n

Case 2

X is an matrix then each column of that matrix treated as in the vector case

Case 2

multidimensional array X is treated as like fft(X) the "first array dimension only if the size of that array is not equal to 1" treated as in the vector case

Y = fft(X,n,dim) gives the Fourier transform along with the dimension dim.



Example Code For ftt() function in MATLAB

Use the help of Fourier transforms to find the frequency components of a signal buried in noise.

Fs = 1000;            % Sampling frequency                    
T = 1/Fs;             % Sampling period       
L = 1500;             % Length of signal
t = (0:L-1)*T;        % Time vector
Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz sinusoid of amplitude 1. S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); Corrupt the signal with zero-mean white noise with a variance of 4. X = S + 2*randn(size(t)); Plot the noisy signal in the time domain. It is difficult to identify the frequency components by looking at the signal X(t). plot(1000*t(1:50),X(1:50)) title('Signal Corrupted with Zero-Mean Random Noise') xlabel('t (milliseconds)') ylabel('X(t)')
Compute the Fourier transform of the signal.

Y = fft(X);

Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L.

P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

Define the frequency domain f and plot the single-sided amplitude spectrum P1. The amplitudes are not exactly at 0.7 and 1, as expected, because of the added noise. On average, longer signals produce better frequency approximations.

f = Fs*(0:(L/2))/L;
plot(f,P1) 
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Now, take the Fourier transform of the original, uncorrupted signal and retrieve the exact amplitudes, 0.7 and 1.0. Y = fft(S); P2 = abs(Y/L); P1 = P2(1:L/2+1); P1(2:end-1) = 2*P1(2:end-1); plot(f,P1) title('Single-Sided Amplitude Spectrum of S(t)') xlabel('f (Hz)') ylabel('|P1(f)|')

OUTPUT For Example of fft () matlab:-

fft () matlab output
fft () matlab output



fftshift matlab
fftshift matlab



fourier transform matlab
fourier transform matlab

Tuesday, November 17, 2020

, ,

Learn about the Matlab linspace function With Example

 Matlab linspace is used to Generate linearly spaced vector.

Syntax for Matlabn Linspace: -

y = linspace(x1,x2)
y = linspace(x1,x2,n)

  • y = linspace(x1,x2) give a row vector which is made with the 100 points with equal distance between x1 and x2
  • y = linspace(x1,x2,n) return the n number of points and these point keep the distance of (x2-x1)/(n-1).
  • This Matlab linspace function is very similar to the colon operator, “:” But linspace function in matlab direct control over the number of points and always include the endpoints.

Example of Matlab linspace


Create a vector of 100 evenly spaced points in the interval [-5,10]. 
y = linspace(-5,10);

Create a vector of 7 evenly spaced points in the interval [-5,7]. 
y1 = linspace(-5,7,7)

output

Example of Matlab linspace
  Example of Matlab linspace

Important points : -

  • x1 and x2 gives the interval for points which is generated by Matlab linspace.
  • Data Types: single | double | datetime | duration
    • Complex Number Support: Yes