plotting equation in matlab - matlab

I am trying to plot equation in matlab for days now and I can't get it look right. This is for school so I know how the end result should look like. I get something similar but not the same.
This is the plot I should get:
under the pic it says that I should use equation 5.8 but I think that it is impossible to get this curve with it because it only gives one result and on plot is show as a dot here is the equation 5.8:
If you read first few lines above it I think it is obvious that there was a printing error and that I should use equation 5.6 (but i am not sure) so here is equation 5.6:
Also the data needed for the equation is given above 5.6. This is my Matlab code for it:
p1=1.581;
p2=-5.534;
p3=0.5523;
om1=1.214;
om2=0.001414;
om3=2.401;
c1=-0.3132;
c2=3.297;
c3=-2.381;
t=0:0.5:5
Ca=0.2132;
Ra=2.275;
V1=(p1*Ca*(sin(om1*t+c1)-om1*Ra*Ca*cos(om1*t+c1)))/(1+(om1^2)*(Ra*Ca)^2)
V2=(p2*Ca*(sin(om2*t+c2)-om2*Ra*Ca*cos(om2*t+c2)))/(1+(om2^2)*(Ra*Ca)^2)
V3=(p3*Ca*(sin(om3*t+c3)-om3*Ra*Ca*cos(om3*t+c3)))/(1+(om3^2)*(Ra*Ca)^2)
V=V1+V2+V3
plot(t,V,'.')
Here is what I get:
So instead of zero at t=0 i get a negative number, and same holds for t=4:5. What is my error is it the wrong equation or is my code bad?

Here is a plot using your code, except that I used
t=0:0.05:5;
which looks quite similar to the book's figure. I agree that the exponential term is probably the reason for the difference close to zero.

Related

I have some problems with the derivative in Matlab

In MATLAB:
Using the X and Y values below, write a MATLAB function SECOND_DERIV in MATLAB. The output of the function should be the approximate value for the second derivative of the data at x, the input variable of the function.
Use the forward difference method and interpolate to get your final answer;
X=[1,1.2,1.44,1.73,2.07,2.49,2.99,3.58,4.3,5.16,6.19,7.43,8.92,10.7,12.84,15.41,18.49];
Y=[18.89,19.25,19.83,20.71,21.96,23.6,25.56,27.52,28.67,27.2,19.38,-2.05,-50.9,-152.82,-354.73,-741.48,-1465.11];​
This is my coding:
function output = SECOND_DERIV(R)
X=[1,1.2,1.44,1.73,2.07,2.49,2.99,3.58,4.3,5.16,6.19,7.43,8.92,10.7,12.84,15.41,18.49];
Y=[18.89,19.25,19.83,20.71,21.96,23.6,25.56,27.52,28.67,27.2,19.38,-2.05,-50.9,-152.82,-354.73,-741.48,-1465.11];
%forward difference method first time.
XX=X(1:end-1)
%first derivative.
dydx=diff(Y)./diff(X)
%second derivative.
dydx2=diff(dydx)
%forward difference method second time.
XXX=XX(1:end-1)
%get the second derivative from input x.
output= interp1(XXX,dydx2,x,'linear','extrap')
end
I do not know what wrong with it.
This is the result I got from my course's web
First, there is no "the" approximate value but rather only "an" approximate value among an infinite set of approximation schemes. In that sense your excercise is ill-defined (but, to be fair, there is probably something you had in the lessons, that completes information).
Using forward differences twice is almost as bad an approximation as it can get. With each forward difference you are displacing the abscissa of the preferred (central difference) approximation by half a sample distance towards the "past".
For the first difference this can be justified by the fact that you might want to stick with the original X-samples. But in the second step you introduce a second displacement by half a sample distance. In order to keep approximation error at least reasonably low, the least you can do is to correct the displacement afterwards by one sample distance towards the "future". This doesn't bring you exactly back to central differences because of non-equidistance, but it's the minimal correction that should be done for the sake of accuracy.
Hence I would replace
XXX=XX(1:end-1)
by
XXX=XX(2:end)
But again, like so many school excercises, the problem is ill-defined and it is difficult to tell from the distance, if this is what is expected from you.

finding power spectrum of signal using two approaches

I am trying to find power spectrum of the signal. The length of the signal is 100000, sample frequency is 1000Hz,and the number of points is 100000. I found the power spectrum using two approaches. The first one is by taking all the length as one part and found power spectrum for it while the second approach is by dividing the signal into 100*1000and find spectrum for each row then get the mean for all rows. My problem is that I must get the same answer in both approaches but I got different answers. I do not know what is the error in my code.
N=100000;
SF=1000;
a=0.1;
b=0.3;
amplitude1=1;
amplitude2=0.5;
t=0:1/SF:100;
f1=SF*a;
f2=SF*b;
A=amplitude1*sin(2*pi*f1*t)+amplitude2*sin(2*pi*f2*t);
Y=2*randn(1,length(A))+A;
bin=[0 :N/2];
fax_Hz=(bin*SF)/N;
FFT=fft(Y);
spectra=2/(SF*length(Y))*(FFT.*conj(FFT));
plot(fax_Hz,spectra(1,1:50001));
D=reshape(Y(1,1:100000),[100,1000]);
M=length(D(1,:));
for i=1:100
FFT_1(i,:)=fft(D(i,:));
S(i,:)=(2/(SF*M))*(FFT_1(i,:).*conj(FFT_1(i,:)));
end
S_f=mean(S);
figure
plot (S_f);
I just update the code. I do not know but when I added noise to signal the two plots looks shifted.
The main problem is with reshape you are working with each row being a separate sequence. Reshape however fills the first column before moving to the second one.
You can use the following instead.
D=reshape(A(1,1:100000),[1000,100]).';
Normalization is another problem. You can either use ifft instead of fft as it is normalized by default (not sure why). Or alternatively keep your normalization and instead of using mean you should can use sum, maybe that is due to a mistake you might have made. There still seems to be a small discrepancy in the amplitudes, not sure where that is coming from.
At the end to plot use the following:
bin=[0 :N];
fax_Hz=(bin*SF)/N;
FFT=ifft(A);
spectra=FFT.*conj(FFT);
plot(fax_Hz,spectra); hold on
D=reshape(A(1,1:100000),[1000,100]).';
M=length(D(1,:));
for i=1:100
FFT_1(i,:)=ifft(D(i,:));
S(i,:)=FFT_1(i,:).*conj(FFT_1(i,:));
end
S_f=mean(S);
plot(fax_Hz(1:100:end-1), S_f);
Note: the fax_Hz(1:100:end-1) is a hacky way of getting the length of the vectors to be the same.

Integrating and Plotting Polynomials in MATLAB

I have a points for a given polynomial. I would like to integrate, preferably using a definite integral, but I believe in the syntax of using polyint this isn't possible without some manipulation. Regardless, if I can just get it to integrate I'll be able to take it from there.
dpt=coeffvalues(fitresult{4});
ppval=polyval(dpt,xx)
cpdt=coeffvalues(fitresult{2});
cpval=polyval(cpdt,xx)
pint=(ppval./cpval);
intp=polyint(pint);
I've tried doing this a couple of ways...One being fitting the results of the pint curve, finding the coefficients and then using the polyint function. But no matter which way I do it I always get the same three errors:
Error using ./
Matrix dimensions must agree.
Error in polyint (line 16)
pi = [p./(length(p):-1:1) k];
Error in ptintegrate97 (line 61)
intp=polyint(ptint);
Usually its the first error that is causing the problem, but when I do size(ppval) and size(cpval), they are both 837x1. So I'm kinda lost. I'm new to MATLAB sorry if this is a stupid question.
polyint won't work here, because it is expecting a series of polynomial coefficients, but you are providing a series of numbers that are the output of the previous calculation and have no relation to any polynomial coefficients whatsoever. The error you are getting is because the shape of pint is wrong. But even if it was right, you wouldn't get the answer you want.
You can choose to integrate pint numerically if you want. Using a simpson's rule on the pint values could certainly get you to a correct answer if your step size between points is small enough. Or, you could return to doing a symbolic polynomial division in order to get an absolute integral. I am not sure what exactly you are after, or what your requirements are.

Plotting and finding roots of bessel functions

I am trying to plot roots of a function that is composed of multiple bessel functions being added and multiplied in Matlab. The equation is Jm(omega)*Ik(omega)+Im(omega)*Jk(omega) where Jm is the bessel function of the first kind of order m (besselj). Im is the modified bessel function of the first kind of order m (besseli). For each mode m=o,1,2,...and n=1,2,3... The frequency omega(mn) is the corresponding root of the listed equation. m=0,1,2 n-1,2,3,4. I need to solve the equation for the 12 roots. I am new to Matlab and this is a little out of my league. So far I have this code but I wasn't sure if I needed the variable omega in the script or not. I have also looked at other people's questions on the suject but didn't see any quite like this. The plots I have seen look nothing like mine which tells me I am probably wrong. Thanks for any help.
m=(0:2); k=(1:3); n=(1:4);
Jm=besselj(m,n');
Ik=besseli(k,n');
Jk=besselj(k,n');
Im=besseli(m,n');
g=Jm.*Ik+Im.*Jk
plot(g)
Plotting
besselj and besseli take what you call omega as their second parameter, so to plot your function you should try something like
m=0; k=1; omega=0:0.02:10;
Jm=besselj(m,omega);
Ik=besseli(k,omega);
Jk=besselj(k,omega);
Im=besseli(m,omega);
g=Jm.*Ik+Im.*Jk;
plot(omega,g);
hold all;
plot(omega,0,'k');
axis([min(omega) max(omega) -100 100]);
This shows you that for m=1, k=1 the first zeros are around 3.2, 6.3 and 9.4:
Finding the roots numerically
You could implement Halley's method for your function g, similar to how the roots of besselj are determined in the MatlabCentral file linked by Cheery.

newton raphson method in matlab

I would like to solve one equation in Matlab with two unknown variables using the Newton raphson method.
The equation is
I(:,:,K) = IL(:,:,K)-Io(:,:,K)*(exp((V+I*Rs)/a(:,:,K))-1)-((V+I*Rs(:,:,K))/Rsh(:,:,K));
Can this be done in matlab and if so please guide me since I have not managed to find anything related to this equation form!
Thanks
No. In general, one equation in two unknowns has an infinite number of solutions. (Think of a contour plot. You are essentially looking for the level set, the locus of all points that yields zero for the dependent variable. It will be a curvilinear path in those variables.)
So you can't "solve" it. I would suggest a good solution to visualize the locus is indeed the function contour. ezplot will do it even better.