My question is about the difference between these two definitions of dirac delta function in Matlab: dirac(t,1) and dirac(t-1) I tried to apply the laplace transform and inverse laplace transform to these 2 functions and they give me very different results:
syms s t
F_s=s+s^2; % definition of the function in s domain
f_t=ilaplace(F_s)
f_t =
dirac(t, 1) + dirac(t, 2)
F_s=laplace(f_t)
F_s =
s^2 + s
F_s=laplace(dirac(t-1)+dirac(t-2)) % but if I use this definition for dirac delta function it gives a very diffrent answer...
F_s =
exp(-s) + exp(-2*s)
and my problem is that when i want to plot the function as:
t=1:0.1:4;
f_t1=dirac(t, 1) + dirac(t, 2);
Error using double.dirac Too many input arguments.
It can't plot the function so I used the definition as :
dirac(t-1)+dirac(t-2) but it gives a very different answer in Laplace transform... could you please explain the reason to me?
Thank you all
sahar
Related
How to fit curves in Simulink and still generate the code?
I have a model of a sensor in Simulink, that returns vectors Pos_x and Pos_y. Each vector has size of 20x1, and their values change every one step time (1ms).
I am trying to calculate the coeffecients of the 3rd degree Polynomial y(x) = p1*x^3 + p2*x^2 + p3*x + p4 in Simulink, that fits the data.
I did not find any block in simulink that calculates the coeffecients, so I used a simple Matlab function
function [p1, p2, p3, p4] = fcn(x,y)
% f(x) = p1*x^3 + p2*x^2 + p3*x + p4
f = fit(x', y', 'Poly3'); % I have also tried "polyfit"
p1 = f.p1;
p2 = f.p2;
p3 = f.p3;
p4 = f.p4;
end
but I get the error:
Function 'fit' not supported for code generation.
1- so back to my qustion, How to fit curves in Simulink and still generate the code?
2- I am also open to any other suggestion
If possible, you can use directly the LSQCURVEFIT function using Levenberg-Marquardt - method, which is Coder-compatible, with some limitations - see it's documentation that describes the limitations in detail.
Under the hood FIT uses LSQCURVEFIT, but in a non-Coder-compatible way.
I am using a rational function (polynomial ratio) to approximate Theodorsen's function. I am having issues in guessing the correct (most optimal) initial guess parameters using the lsqcurvefit function in Matlab. Is there a way to know what would be the best initial guess parameters to get the best fit result?
My code is the following:
k = logspace(-10,1,300);
% Exact Theodorsen's Function C(k) in terms of Bessel Function
Ck= #(k)(besselh(1,2,k))./(besselh(1,2,k)+1i*besselh(0,2,k));
% Ckget function used to extract real and imaginary numbers and stacks the result
Ckget= #(k)[real(Ck(k)); imag(Ck(k))];
% Define 3rd order approximation function
x0= [.1,.1,.1,1,.1,.1,.1,.1]; % Create initial guess
U = 200;
b = 3;
s = 1i*k*(U/b);
Ck2 = #(x,k)((x(1)*s.^3 + x(2)*s.^2 + x(3)*s + x(4))./(x(5)*s.^3 + x(6)*s.^2 + x(7)*s + x(8)));
Ck2get= #(x,k)[real(Ck2(x,k)); imag(Ck2(x,k))]; %extract real & imaginary values
% Use curve fit function for best fit approximation
x2= lsqcurvefit(Ck2get,x0,k,Ckget(k))
f(t) = t*e^t when 0 <= t < 3
f(t) = 0 when 3 <= t
how to use Matlab to find Laplace transforms of functions which change according to different values of t
MATLAB has a function called laplace, and we can calculate it like:
syms x y
f = 1/sqrt(x);
laplace(f)
But it will be a very long code when we turn f(x) like this problem into syms.
Indeed, we can do this by using dirac and heaviside if we have to. Nevertheless, we could use this instead:
syms t s
f=t*exp((1-s)*t);
F=int(f,t,0,3)
It is because:
If you are interested in a numerical implementation of the Laplace transform, you can download from Matlab's file exchange the following Numerical Transform and the inverse transform ...
OK let me cut to the chase.
I am trying to use MATLAB to
(i)generate the fourier series based on known coefficients and thereafter
(ii) determine the output function when the impulse is known.
So far I used this code to obtain the fourier series:
clear all
syms x k L n
evalin(symengine,'assume(k,Type::Integer)');
a = #(f,x,k,L) (2/(pi*k))* sin((pi*k)/(2 * L));
fs = #(f,x,n,L) (1/2*L) + symsum(a(f,x,k,L)*cos(k*2*pi*x/L),k,1,n);
f = x;
pretty(fs(f,x,11,1))
This works as desired. Now the impulse response is as follows:
h = heaviside(x) * exp(-5*x);
Now, in order to obtain the function, we need to perform the convolution with the respective functions.But when I input the following, I get the error:
x1 = fs(f,x,1,1);
conv(h,x1)
Undefined function 'conv2' for input arguments of type 'sym'.
Error in conv (line 38)
c = conv2(a(:),b(:),shape);
Any help would be appreciated
That is because conv is only defined for numeric inputs. If you want to find the convolution symbolically, you'll have to input the equation yourself symbolically using integration.
If you recall, the convolution integral is defined as:
Source: Wikipedia
Therefore, you would do this:
syms x tau;
F = int(h(tau)*x1(x-tau),'tau',-inf,+inf);
int is a function in MATLAB that does symbolic integration for you. Also note that the convolution integral is commutative, and so this also works:
Source: Wikipedia
Therefore, you should also get the same answer if you did:
syms x tau;
F = int(h(x-tau)*x1(tau),'tau',-inf,+inf);
Hope this helps!
I have a data-set which is loaded into matlab. I need to do exponential fitting for the plotted curve without using the curve fitting tool cftool.
I want to do this manually through executing a code/function that will output the values of a and b corresponding to the equation:
y = a*exp(b*x)
Then be using those values, I will do error optimization and create the best fit for the data I have.
Any help please?
Thanks in advance.
Try this...
f = fit(x,y,'exp1');
I think the typical objective in this type of assignment is to recognize that by taking the log of both sides, various methods of polynomial fit approaches can be used.
ln(y) = ln(a) + ln( exp(x).^b )
ln(y) = ln(a) + b * ln( exp(x) )
There can be difficulties with this approach when errors such as noise are involved due to the behavior of ln as it approaches zero.
In this exercise I have a set of data that present an exponential curve and I want to fit them exponentially and get the values of a and b. I used the following code and it worked with the data I have.
"trail.m" file:
%defining the data used
load trialforfitting.txt;
xdata= trialforfitting(:,1);
ydata= trialforfitting(:,2);
%calling the "fitcurvedemo" function
[estimates, model] = fitcurvedemo(xdata,ydata)
disp(sse);
plot(xdata, ydata, 'o'); %Data curve
hold on
[sse, FittedCurve] = model(estimates);
plot(xdata, FittedCurve, 'r')%Fitted curve
xlabel('Voltage (V)')
ylabel('Current (A)')
title('Exponential Fitting to IV curves');
legend('data', ['Fitting'])
hold off
"fitcurvedemo.m" file:
function [estimates, model] = fitcurvedemo(xdata, ydata)
%Call fminsearch with a random starting point.
start_point = rand(1, 2);
model = #expfun;
estimates = fminsearch(model, start_point);
%"expfun" accepts curve parameters as inputs, and outputs
%the sum of squares error [sse] expfun is a function handle;
%a value that contains a matlab object methods and the constructor
%"FMINSEARCH" only needs sse
%estimate returns the value of A and lambda
%model computes the exponential function
function [sse, FittedCurve] = expfun(params)
A = params(1);
lambda = params(2);
%exponential function model to fit
FittedCurve = A .* exp(lambda * xdata);
ErrorVector = FittedCurve - ydata;
%output of the expfun function [sum of squares of error]
sse = sum(ErrorVector .^ 2);
end
end
I have a new set of data that doesn't work with this code and give the appropriate exponential fit for the data curve plotted.