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.
Related
I'm trying to numerically integrate a discontinuous function in Matlab.
fun = #(z) 1./((z+1).^2+4) * 1./(exp(-z)-1);
q = integral(fun,-Inf,Inf,'Waypoints',[0])
There is a discontinuity at z=0, but I'm not sure how to use the Waypoints option to specify this. I get an error message:
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 7.4e-01. The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.
How can I calculate this integral accurately?
From the docs "Use waypoints to indicate any points in the integration interval that you would like the integrator to use."
Probably the only point in that equation you don't want to use us zero.... as the function is undefined at that value (its limits from the left and rigth are different, thus its not infinite, its undefined).
Wolfram Alpha claims that the integral does not exists.
So, when MATLAB says
"The integral may not exist, or it may be difficult to approximate
numerically to the requested accuracy"
It is.... because it may not exist!
I guess you could always do something like:
q = integral(fun,-Inf,-0.001)+integral(fun,0.001,Inf);
but I am not sure how correct this is....
Adding a remark to #Ander Biguri's correct answer.
There are other functions with singularities, e.g. 1/((x+1)*sqrt(x)) (look at improper integrals)
integral(#(x) 1./((x+1).*sqrt(x)), 0, inf)
ans =
3.1416
and even function that converge with singularities not at the border of the integrating range
integral(#(x) 1./(x.^2).^(1/3), -1, 1)
ans =
6.0000
So MATLAB does everything right. Your function does not converge.
Maybe you are interested in Cauchy's principal value, but this is another topic.
I am trying to solve a large DAE system, coupled with the equations to calculate the sensitivity of the variables to a parameter. My problem is the jacobian of the entire system, its calculation is pretty slow and I would like to speed it up.
I am using numjac, in this form:
[Jx,FAC,G] = numjac(#(t,y)MODEL(t,y,X),tt,yy,dydt,jac_tol,FAC,0,JPat,G);
I want to vectorize the code, but I can't seem to get what this means. As far as I understood my code is already vectorized! t,y,X go in and I get a column vector of dy(i)/dt, or F(t,y(i)). But if I say that my function is vectorized, I get an error:
Matrix dimensions must agree.
Error in numjac (line 192)
Fdiff = Fdel - Fty(:,ones(1,ng));
How can I properly vectorize it?
I don't know how to set the intevals of a integral to get the best precise result.
For example, this is the orginal definition of the formula.
y=integral(#(x) log2((f1(x))./(f2(x))), -inf, inf).
Note: f1(x)->0 and f2(x)->0 when x->-inf or inf, and the decreasing speeds are different.
If I use [-inf, inf] Matlab gives me NaN.
If I narrow down the inteval, Matlab gives a number. But if I increas the inteval a little bit, I get another number. So I am wondering how to deal this kind of integral calculation? How to make it as precise as possible without NaN?
Thanks a lot.
I don't think your integral converges for the definitions you have given. For example, for N=1 the integrand simplifies to (1/2 - 2*x)/log(2), which is clearly nonconverging at infinity. For larger N the integrand goes to -inf for x->inf and to inf for x->-inf, and I don't think the integral converges either, though I do not have a full proof at the moment.
It is good practice to examine mathematical functions analytically before running numerical analysis. If this is not possible, then try first plotting the function itself over the relevant range to get an idea of its behavior. A good way to plot functions over many orders of magnitude is by using the logspace function for x values.
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.
I'm not too familiar with MATLAB or computational mathematics so I was wondering how I might solve an equation involving the sum of squares, where each term involves two vectors- one known and one unknown. This formula is supposed to represent the error and I need to minimize the error. I think I'm supposed to use least squares but I don't know too much about it and I'm wondering what function is best for doing that and what arguments would represent my equation. My teacher also mentioned something about taking derivatives and he formed a matrix using derivatives which confused me even more- am I required to take derivatives?
The problem that you must be trying to solve is
Min u'u = min \sum_i u_i^2, u=y-Xbeta, where u is the error, y is the vector of dependent variables you are trying to explain, X is a matrix of independent variables and beta is the vector you want to estimate.
Since sum u_i^2 is diferentiable (and convex), you can evaluate the minimal of this expression calculating its derivative and making it equal to zero.
If you do that, you find that beta=inv(X'X)X'y. This maybe calculated using the matlab function regress http://www.mathworks.com/help/stats/regress.html or writing this formula in Matlab. However, you should be careful how to evaluate the inverse (X'X) see Most efficient matrix inversion in MATLAB