How long does it take for a curve too reach a specific x coordinate? - matlab

How do I calculate the time it takes for a curve to reach a specific x coordinate (in Matlab). Let's say we have:
dx/dt = x^2 + y^2 and dy/dt = 5.x.y and the curve starts at the point (a,b). With help from ode45 I was able to get the figure of the curve. I need too calculate the time it takes for the curve too reach x = c, (c>a). I've been told that this can be done by interpolation, but I have no idea how to write the code.

Depending on the behavior of your system around c, using data interpolation methods such as interp1 on the output may or may not work. The more rigorous way to solve this is either with events (see my answers here or here) or by using the single structure output argument form of ode45 in conjunction with deval and regular data interpolation methods. Both of these use polynomial interpolation methods designed to work with the underlying ODEs. Though more complicated, events are probably the best way to accuratly determine crossing times like your case.

Related

Double numerical integration in Matlab - Singularity

I have discrete data of a 2D function defined as
theta = linspace(0,pi,nTheta);
phi = linspace(0,2*pi,nPhi);
p=zeros(nPhi,nTheta);%only to show the dimension of my matrix
[np,nt]=ndgrid(phi,theta);
f1 = griddedInterpolant(np,nt,p,'spline');
f2= #(np,nt) f1(np,nt);
integral2(f2,0,2*pi,0,pi)
Note that p is calculated from a complex physical problem, but i showed above how it is initialized.
Also, I can increase nTheta and nPhi, which leads to more accurate calculation of p.
My calculated function (with nPhi=400,nTheta=200) is something like:
I tried 3 ways :
using Trapz function
using the code above but with linear interpolation for gridded interpolant
using the code above with spline interpolation
Although the spline is better than others, i still need to increase nPhi and nTheta, which makes it impossible for me to do the simulation due to its cost.
Is there any suggestion except these 3 methods or any general suggestion how i can do this computation more efficient? (I also took advantage of the symmetry in both directions)
Note that the shape of my function varies in each time step, so a local mesh refinement might be challenging because i don't know the detail of my function in advance.

cftool not fitting on custom fit

I'm trying to do a fit on cftool for a basic oscillator. The problem is that Matlab won't make a fit; it keeps drawing a straight line. I've been experimenting with the starting points and limits, but to no avail.
The problem is probably something trivial, but I can't figure out the problem.
Current fit:
You are using custom equation y = f(x) = a * exp(-b*x) * sin(dx+e) + c.
Matlab understands dx inside the sin above as a constant coefficient, so you have the sin of a constant, which is a constant number itself.
cftool is left then trying to approximate a sinusoidal motion with f(x), which at this point is a custom exponential function of the type const * exp(-const * x) + const, so the best it can do is to yield the mean value, that is, ~0.176.
In order to correct this, just substitute d*x for dx inside the sin in your custom function.
In addition to the pertinent answer from Lingo.
In practical use of non-linear regression sofware, a frequent cause of failure or of not good convergence is the initial setting of values of the parameters. The values of parameters given below would be very good to start a non-linear regression calculus.
Those values are probably more or less biaised because the data was not available on numerical form but only from the graph provided by the OP. "Substitue" data was obtained from scanning the graph. This isn't an accurate way.
NOTE : The linearised regression method used to compute the above approximate values is explained in https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

Solving a pair of real (and interwined) equations

I have a first-order difference equation: y[n] = z0 * [n-1] + x[n] (2-3). Usually what we would do is apply the z-transform, then use the "filter" function. But my teacher wants to do it differently:
In the first-order difference equation (2-3), let yR[n] and yI[n]
denote the real and imaginary parts of y[n]. Write a pair of
real-valued difference equations expressing yR[n] and yI[n] in terms
of yR[n-1], yI[n-1], x[n], and r, cos m, and sin m
(I forgot to mention, x[n]=G*dirac[n] where G is a complex constant, which is where r, cos m and sin m came from).
Here is my result (this is the best I could think of):
yR[n]=r(yR[n-1]cosm - yI[n-1]sinm) + xR[n]
yI[n]=r(yI[n-1]cosm + yR[n-1]sinm) + xI[n]
Then the next question is:
Write a MATLAB program to implement this pair of real equations, and
use this prorgam to generate the impulse response of equation (2-3)
for r=1/2 and m=0, and m=pi/4. For these 2 cases, plot the real part
of the impulse responses obtained. Compare to the real part of the
output from the complex recursion (2-3)
What I don't understand is just how i can do this besides applying z-transform and then use the "filter" function. I have looked up on the web, and there was something about the state-space form, but I don't know if it's relevant or not. Also I'm not looking to have the solution handed to me on a silver platter, I just want to know how to work it out. Thank you very much!
You are on the right track. For a digital system, such as you have, you simply set the initial input, and run the program. There is no need to do anything fancy, you are very much overthinking the problem. In other words, for a simple function, you could do this:
f(0)=1;
f(n)=a*f(n-1);
Essentially for this you would loop for some range (Maybe 20 points), where f(n) looks at the previous function.
For your function, I suspect you simply set the real part (yR[0]) to 1, yI[0]=0, and run it for a while.
I know Matlab is 1 based, so you probably want to actually set the first value to 1, but the same principal applies.

Using matlab to obtain the vector fields and the angles made by the vector field on a closed curve?

Here is the given system I want to plot and obtain the vector field and the angles they make with the x axis. I want to find the index of a closed curve.
I know how to do this theoretically by choosing convenient points and see how the vector looks like at that point. Also I can always use
to compute the angles. However I am having trouble trying to code it. Please don't mark me down if the question is unclear. I am asking it the way I understand it. I am new to matlab. Can someone point me in the right direction please?
This is a pretty hard challenge for someone new to matlab, I would recommend taking on some smaller challenges first to get you used to matlab's conventions.
That said, Matlab is all about numerical solutions so, unless you want to go down the symbolic maths route (and in that case I would probably opt for Mathematica instead), your first task is to decide on the limits and granularity of your simulated space, then define them so you can apply your system of equations to it.
There are lots of ways of doing this - some more efficient - but for ease of understanding I propose this:
Define the axes individually first
xpts = -10:0.1:10;
ypts = -10:0.1:10;
tpts = 0:0.01:10;
The a:b:c syntax gives you the lower limit (a), the upper limit (c) and the spacing (b), so you'll get 201 points for the x. You could use the linspace notation if that suits you better, look it up by typing doc linspace into the matlab console.
Now you can create a grid of your coordinate points. You actually end up with three 3d matrices, one holding the x-coords of your space and the others holding the y and t. They look redundant, but it's worth it because you can use matrix operations on them.
[XX, YY, TT] = meshgrid(xpts, ypts, tpts);
From here on you can perform whatever operations you like on those matrices. So to compute x^2.y you could do
x2y = XX.^2 .* YY;
remembering that you'll get a 3d matrix out of it and all the slices in the third dimension (corresponding to t) will be the same.
Some notes
Matlab has a good builtin help system. You can type 'help functionname' to get a quick reminder in the console or 'doc functionname' to open the help browser for details and examples. They really are very good, they'll help enormously.
I used XX and YY because that's just my preference, but I avoid single-letter variable names as a general rule. You don't have to.
Matrix multiplication is the default so if you try to do XX*YY you won't get the answer you expect! To do element-wise multiplication use the .* operator instead. This will do a11 = b11*c11, a12 = b12*c12, ...
To raise each element of the matrix to a given power use .^rather than ^ for similar reasons. Likewise division.
You have to make sure your matrices are the correct size for your operations. To do elementwise operations on matrices they have to be the same size. To do matrix operations they have to follow the matrix rules on sizing, as will the output. You will find the size() function handy for debugging.
Plotting vector fields can be done with quiver. To plot the components separately you have more options: surf, contour and others. Look up the help docs and they will link to similar types. The plot family are mainly about lines so they aren't much help for fields without creative use of the markers, colours and alpha.
To plot the curve, or any other contour, you don't have to test the values of a matrix - it won't work well anyway because of the granularity - you can use the contour plot with specific contour values.
Solving systems of dynamic equations is completely possible, but you will be doing a numeric simulation and your results will again be subject to the granularity of your grid. If you have closed form solutions, like your phi expression, they may be easier to work with conceptually but harder to get working in matlab.
This kind of problem is tractable in matlab but it involves some non-basic uses which are pretty hard to follow until you've got your head round Matlab's syntax. I would advise to start with a 2d grid instead
[XX, YY] = meshgrid(xpts, ypts);
and compute some functions of that like x^2.y or x^2 - y^2. Get used to plotting them using quiver or plotting the coordinates separately in intensity maps or surfaces.

Matlab recursive curve fitting with custom equations

I have a curve IxV. I also have an equation that I want to fit in this IxV curve, so I can adjust its constants. It is given by:
I = I01(exp((V-R*I)/(n1*vth))-1)+I02(exp((V-R*I)/(n2*vth))-1)
vth and R are constants already known, so I only want to achieve I01, I02, n1, n2. The problem is: as you can see, I is dependent on itself. I was trying to use the curve fitting toolbox, but it doesn't seem to work on recursive equations.
Is there a way to make the curve fitting toolbox work on this? And if there isn't, what can I do?
Assuming that I01 and I02 are variables and not functions, then you should set the problem up like this:
a0 = [I01 I02 n1 n2];
MinFun = #(a) abs(a(1)*(exp(V-R*I)/(a(3)*vth))-1) + a(2)*(exp((V-R*I)/a(4)*vth))-1) - I);
aout = fminsearch(a0,MinFun);
By subtracting I and taking the absolute value, the point where both sides are equal will be the point where MinFun is zero (minimized).
No, the CFTB cannot fit such recursively defined functions. And errors in I, since the true value of I is unknown for any point, will create a kind of errors in variables problem. All you have are the "measured" values for I.
The problem of errors in I MAY be serious, since any errors in I, or lack of fit, noise, model problems, etc., will be used in the expression itself. Then you exponentiate these inaccurate values, potentially casing a mess.
You may be able to use an iterative approach. Thus something like
% 0. Initialize I_pred
I_pred = I;
% 1. Estimate the values of your coefficients, for this model:
% (The curve fitting toolbox CAN solve this problem, given I_pred)
I = I01(exp((V-R*I_pred)/(n1*vth))-1)+I02(exp((V-R*I_pred)/(n2*vth))-1)
% 2. Generate new predictions for I_pred
I_pred = I01(exp((V-R*I_pred)/(n1*vth))-1)+I02(exp((V-R*I_pred)/(n2*vth))-1)
% Repeat steps 1 and 2 until the parameters from the CFTB stabilize.
The above pseudo-code will work only if your starting values are good, and there are not large errors/noise in the model/data. Even on a good day, the above approach may not converge well. But I see little hope otherwise.