Interpolate on scattered data in MATLAB - matlab

I have some series of data (y axis) vs time (x axis) relationship. I want to find specific time value of a specific "y value" by interpolating. I tried to use interp1 command. But the problem is, the curve passes that "y value" at several time values. interp1 command only gives the first time value. is there any way of performing interpolation so that i can find every time value?
thanks!

this is not a smart answer.
essentially you are trying to solve
y(t)=c
this can be written as
f(t)=y(t)-c = 0
then it remains to find the zero-crossings of function f(t) i guess there are functions to detect zero crossings or find roots of a dataset in matlab like fzero fnzero or crossing. but here is a home-made one to find points where value of a sine function is 0.5
t=1:50;
y=sind(4*pi.*t);
plot(t,y);
hold on;
t1=1:0.02:50;
y1=interp1(t,y,t1,'spline');
plot(t1,y1,'k');
c=0.5;
f=y1-c;
nn=length(y1);
k=0;
for i=1:nn-1
if f(i)*f(i+1) < 0
k=k+1;
allzeros(k)=i;
end
end
plot(t1(allzeros),y1(allzeros),'+');

Related

How do I get the values of y and y' for specific values of t from ode45 in Matlab?

I have to solve a second-degree differential equation and I specifically need the value of the first derivative of y at the final time point. My code is the following:
[T Y]=ode45(#(t y)vdp4(t,y,0.3),[0 1],[0.3/4,((3*0.3)^0.5)/2]);
I know the output will contain the values at which ode45 evaluated the function. To get the y values at specific time value at have it has been advised to give more than two time points in the MATLAB documentation. I did that too.
tspan=[0:0.01:1]
[T Y]=ode45(#(t y)vdp4(t,y,0.3),tspan,[0.3/4,((3*0.3)^0.5)/2]);
The T vector still does not have all the values from 0 to 1 (The last value is 0.39). This happens especially after multiple executions of ode45 function. I found something else in the MATLAB documentation: using "sol" structure to deval the values for specific t values. Is that the right way to go?
For reference, my differential equation is in the following function.
function dy = vdp4(t,y,k)
dy = zeros(2,1); % a column vector
dy(1)=y(2);
dy(2)=(y(2)^2-2*t*y(2)+2*y(1))/k+2;
end
Edit: I provided the parameter value. It should now be executable.
Try to plot your solution, you will find the answer

Q on plotting function against t and trajectory in phase space(matlab)

I am very beginner for matlab and try to solve this question. But so far it is not successful. I have spent quite a time and I think I need some help. I would appreciate any help!!!
I need to plot v against time and trajectory of v and w in phase space. The whole question is below and my code for previous question connected to this question is also below. I can go with subplot(2,1,1) for the first graph and subplot(2,1,2) for the next graph. But I am not sure what I have to do other than this. I kind of found ode45 command. But not sure how to use it and if it is the right one to use here. I have tried to use ode45. But it shows many errors that I don't understand.....Please help me on this. Thanks a lot!
'Create a figure which contains two graphs, using subplot. In the first graph, plot the temporal evolution of the membrane potential v(t) against time t. In the second graph, plot the corresponding trajectory (v(t); w (t)) in (the so-called) phase space.'
% my code is below.
a=0.08;
b=3.6;
c=0.7;
T=2; % this can be any number
I_ext=20; % this can be any number
dt=0.01; % this can be any number
function [ v,w ] = fhnn( a,b,c,I_ext,T,dt)
v=zeros(1,numel(T/dt));
w=zeros(1,numel(T/dt));
for n=1:numel(T/dt)
v(n+1)=(v(n)-v(n)^3/3-w(n)+I_ext)*dt+v(n);
w(n+1)=a*(v(n)-b*w(n)+c)*dt+w(n);
end
I gather you have a differential equation and are trying to directly plot that. You might find a better approach would be to actually solve the equation if that is possible.
Either way, recognising that:
numel returns the length of an array and dT/dt is always a scalar so the length is always one.
fhnn is not used here.
You still need a vector t.
If what is in your for loop is correct, the following should work:
a=0.08; b=3.6; c=0.7; T=2; I_ext=20; dt=0.01;
t = 0:dt:T;
v = zeros(1,round(T/dt));
w = zeros(1,round(T/dt));
for n=1:round(T/dt)-1
v(n+1)=(v(n)-v(n)^3/3-w(n)+I_ext)*dt+v(n);
w(n+1)=a*(v(n)-b*w(n)+c)*dt+w(n);
end
subplot(2,1,1)
plot(t,v)
xlabel('t')
ylabel('v')
subplot(2,1,2)
plot(v,w)
xlabel('v')
ylabel('w')

How to have square wave in Matlab symbolic equation

My project require me to use Matlab to create a symbolic equation with square wave inside.
I tried to write it like this but to no avail:
syms t;
a=square(t);
Input arguments must be 'double'.
What can i do to solve this problem? Thanks in advance for the helps offered.
here are a couple of general options using floor and sign functions:
f=#(A,T,x0,x) A*sign(sin((2*pi*(x-x0))/T));
f=#(A,T,x0,x) A*(-1).^(floor(2*(x-x0)/T));
So for example using the floor function:
syms x
sqr=2*floor(x)-floor(2*x)+1;
ezplot(sqr, [-2, 2])
Here is something to get you started. Recall that we can express a square wave as a Fourier Series expansion. I won't bother you with the details, but you can represent any periodic function as a summation of cosines and sines (à la #RTL). Without going into the derivation, this is the closed-form equation for a square wave of frequency f, with a peak-to-peak amplitude of 2 (i.e. it goes from -1 to 1). Recall that the frequency is the amount of cycles per seconds. Therefore, f = 1 means that we repeat our square wave every second.
Basically, what you have to do is code up the first line of the equation... but how in the world would you do that? Welcome to the world of the Symbolic Math Toolbox. What we will need to do before hand is declare what our frequency is. Let's assume f = 1 for now. With the Symbolic Math Toolbox, you can define what are considered as mathematics variables within MATLAB. After, MATLAB has a whole suite of tools that you can use to evaluate functions that rely on these variables. A good example would be if you want to use this to define a closed-form solution of a function f(x). You can then use diff to differentiate and see what the derivative is. Try it yourself:
syms x;
f = x^4;
df = diff(f);
syms denotes that you are declaring anything coming after the statement to be a mathematical variable. In this case, x is just that. df should now give you 4x^3. Cool eh? In any case, let's get back to our problem at hand. We see that there are in fact two variables in the periodic square function that need to be defined: t and k. Once we do this, we need to create our function that is inside the summation first. We can do this by:
syms t k;
f = 1; %//Define frequency here
funcSum = (sin(2*pi*(2*k - 1)*f*t) / (2*k - 1));
That settles that problem... now how do we encapsulate this into an infinite sum!? The sum command in MATLAB assumes that we have a finite array to sum over. If you want to symbolically sum over a function, we must use the symsum function. We usually call it like this:
funcOut = symsum(func, v, start, finish);
func is the function we wish to sum over. v is the summation variable that we wish to use to index in the sum. In our case, that's k. start is the beginning of the sum, which is 1 in our case, and finish is where we wish to finish up our summation. In our case, that's infinity, and so MATLAB has a special keyword called Inf to denote that. Therefore:
xsquare = (4/pi) * symsum(funcSum, k, 1, Inf);
xquare now contains your representation of a square wave defined in terms of the Symbolic Math Toolbox. Now, if you want to plot your square wave and see if we have this right. We can do the following. Let's go between -3 <= t <= 3. As such, you would do something like this:
tVector = -3 : 0.01 : 3; %// Choose a step size of 0.01
yout = subs(xsquare, t, tVector);
You will notice though that there will be some values that are NaN. The reason why is because right at a multiple of the period (T = 1, 2, 3, ...), the behaviour is undefined as the derivative right at these points is undefined. As such, we can fill this in using either 1 or -1. Let's just choose 1 for now. Also, because the Fourier Series is generally a complex-valued function, and the square-wave is purely real, the output of this function will actually give you a complex-valued vector. As such, simply chop off the complex parts to get the real parts only:
yout = real(double(yout)); %// To cast back to double.
yout(isnan(yout)) = 1;
plot(tVector, yout);
You'll get something like:
You could also do this the ezplot way by doing: ezplot(xsquare). However, you'll see that at the points where the wave repeats itself, we get NaN values and so there is a disconnect between the high peak and low peak.
Note:
Natan's solution is much more elegant. I was still writing this post by the time he put something up. Either way, I wanted to give a more signal processing perspective to how to do this. Go Fourier!
A Fourier series for the square wave of unit amplitude is:
alpha + 2/Pi*sum(sin( n * Pi*alpha)/n*cos(n*theta),n=1..infinity)
Here is a handy trick:
cos(n*theta) = Re( exp( I * n * theta))
and
1/n*exp(I*n*theta) = I*anti-derivative(exp(I*n*theta),theta)
Put it all together: pull the anti-derivative ( or integral ) operator out of the sum, and you get a geometric series. Then integrate and finally take the real part.
Result:
squarewave=
alpha+ 1/Pi*Re(I*ln((1-exp(I*(theta+Pi*alpha)))/(1-exp(I*(theta-Pi*alpha)))))
I tried it in MAPLE and it works great! (probably not very practical though)

Input signal for Simulink

I want to use the following signal (red) in Simulink as input.
All I have is this picture. Any advice on the simplest way to implement this signal?
Your question has two parts: bringing the data to work space of Matlab and feeding the data to Simulink.
For the first part I think the simplest thing is to put about 30 points on the figure and write their estimated (x,y) values in vectors X and Y. it should not be hard because the first part of it is periodic.
Then use plot(X,Y) to plot this vector in Matlab and update your estimated values till you are satisfied that your plot is similar to the figure.
For the second part you can create a structure where time is the same as your X axis and Y as the values:
input.time = X;
input.signals.values = Y;
where X and Y should have the same length.
you can find good examples of how to import signals from work space to Simulink at this page: https://www.mathworks.com/help/simulink/slref/fromworkspace.html

How to plot this equation in MATLAB? Keep getting matrix dimension errors

The code I have is:
T=[0:0.1:24];
omega=((12-T)/24)*360;
alpha = [0:1:90];
latitude=35;
delta=[-23.45:5:23.45];
sind(alpha)=sind(delta).*sind(latitude)+cosd(delta).*cosd(latitude).*cosd(omega)
cosd(phi)=(sind(alpha).*sind(latitude)-cosd(delta))./(cosd(alpha).*cosd(latitude))
alpha represents my y-axis and is an angle between 0 and 90 degrees. phi represents my x-axis and is an angle between say -120 to +120. The overall result should look something like a half sine-wave.
Whenever I try to input that last line I get the error stating inner matrix dimensions must agree. So I tried to use reshape on my matrices for those variables I defined so that they work. But then I get '??? ??? Subscript indices must either be real positive integers or logicals.'
It seems very tedious to have to reshape my matrix every time I define a new set of variables in order to use them with an equation. Those variables are used for defining my axis range, is there a better way I can lay them out or an automatic command that will make sure they work every time?
I want to plot alpha and phi as a graph using something like
plot(alpha,phi)
but can't get past those errors? can't I just use a command that says something like define x-axis [0:90], define y-axis [-120:120] or something? I have spent far too much time on this problem and can't find a solution. I just want to plot the graph. Somebody please help! thanks.
Thanks
Here is a start for you:
% Latitude
L=35;
% Hour Angle
h = [-12:5/60:12];
w = 15*h;
% Initialize and clear plot window
figure(1); clf;
% Plot one day per month for half of the year
for N = 1:30:365/2
% Declination
d = 23.45*sind(360*(284+N)/365);
% Sun Height
alpha = asind(sind(L)*sind(d) + cosd(L)*cosd(d)*cosd(w));
% Solar Azimuth
x = ( sind(alpha)*sind(L)-sind(d) )./( cosd(alpha)*cosd(L) );
y = cosd(d)*sind(w)./cosd(alpha);
phi = real(atan2d(y,x));
% Plot
plot(phi,alpha); hold on;
end
hold off;
grid on;
axis([-180, 180, 0, 90]);
xlabel('Solar Azimuth')
ylabel('Solar Elevation')
The function asind is inherently limited to return values in the range of -90 to 90. That means that you will not get a plot that spans over 240 degrees like the one you linked to. In order for the plot to not have the inflection at +/- 90 degrees, you will need to find a way to infer the quadrant. Update: I added the cosine term to get an angle using atan2d.
Hopefully this will be enough to give you an idea of how to use Matlab to get the kind of result that you are after.
Element-wise multiplication (.*) and division ./, not the matrix versions:
sind(alpha)=sind(delta).*sind(latitude)+cosd(delta).*cosd(latitude).*cosd(omega)
cosd(phi)=(sind(alpha).*sind(latitude)-cosd(delta))./(cosd(alpha).*cosd(latitude))
But a bigger problem is that you can't index with decimal or non-positive values. Go back to the code before you got the subscript indices error. The indices "must either be real positive integers or logicals".
I think you're a bit confused about MATLAB sintax (as nispio mentioned). Maybe you want to do something like this?
T=[0:0.1:24];
omega=((12-T)/24)*360;
alpha = [0:1:90];
latitude=35;
delta=[-23.45:5:23.45];
[o1,d1]=meshgrid(omega,delta);
[a2,d2]=meshgrid(alpha,delta);
var1=sind(d1(:)).*sind(latitude)+cosd(d1(:)).*cosd(latitude).*cosd(o1(:));
var2=(sind(a2(:)).*sind(latitude)-cosd(d2(:)))./(cosd(a2(:)).*cosd(latitude));
plot(var1);hold on;plot(var2);
If not, you should post the algorithm or pseudocode