How to calculate out put definite integral in matlab - matlab

I have run the following for computing the numerical integration of the following function.
But in Matlab instead of a number as an output, it gives me a big expression.
gamma = sqrt(alpha^2 - beta^2);
K1=besselk(1,alpha*sqrt(delta^2+(x-mu)^2));
pdf = alpha*delta* K1/(pi*sqrt(delta^2+(x-mu)^2)) * exp(delta*gamma+ beta*(x-mu));
prob = int(pdf , x, 0,0.5);
with the following parameters :
mu = 0.034 ;
delta = 2.12;
alpha = 0.05;
beta = -0.001;
I have recived this result.
int((53*exp(59679899628370215233/562949953421312000000 - x/1000)*besselk(1, ((x - 17/500)^2 + 2809/625)^(1/2)/20))/(500*pi*((x - 17/500)^2 + 2809/625)^(1/2)), x, 0, 1/2)
I would appreciate any solution to answer this question. How can I compute this expression in Matlab?

The result you got is just not evaluated yet. To obtain a numerical answer with n significant digits, use,
prob_vpa = vpa(prob, n);
The result will still be a symbolic variable, for further symbolic computations. You can also convert it to double,
prob_double = double(prob);

Related

Definite integration using int command

Firstly, I'm quite new to Matlab.
I am currently trying to do a definite integral with respect to y of a particular function. The function that I want to integrate is
(note that the big parenthesis is multiplying with the first factor - I can't get the latex to not make it look like power)
I have tried plugging the above integral into Desmos and it worked as intended. My plan was to vary the value of x and y and will be using for loop via matlab.
However, after trying to use the int function to calculate the definite integral with the code as follow:
h = 5;
a = 2;
syms y
x = 3.8;
p = 2.*x.^2+2.*a.*y;
q = x.^2+y.^2;
r = x.^2+a.^2;
f = (-1./sqrt(1-(p.^2./(4.*q.*r)))).*(2.*sqrt(q).*sqrt(r).*2.*a-p.*2.*y.*sqrt(r)./sqrt(q))./(4.*q.*r);
theta = int(f,y,a+0.01,h) %the integral is undefined at y=2, hence the +0.01
the result is not quite as expected
theta =
int(-((8*461^(1/2)*(y^2 + 361/25)^(1/2))/5 - (461^(1/2)*y*(8*y + 1444/25))/(5*(y^2 + 361/25)^(1/2)))/((1 - (4*y + 722/25)^2/((1844*y^2)/25 + 665684/625))^(1/2)*((1844*y^2)/25 + 665684/625)), y, 21/10, 5)
After browsing through various posts, the common mistake is the undefined interval but the +0.01 should have fixed it. Any guidance on what went wrong is much appreciated.
The Definite Integrals example in the docs shows exactly this type of output when a closed form cannot be computed. You can approximate it numerically using vpa, i.e.
F = int(f,y,a,h);
theta = vpa(F);
Or you can do a numerical computation directly
theta = vpaintegral(f,y,a,h);
From the docs:
The vpaintegral function is faster and provides control over integration tolerances.

Finding the maximum value from an expression using a loop in Matlab

I want to find the maximum value using the second derivative of the the expression when x is between 0 and 1. In other words I am taking the derivative of cox(x^2) twice to get the second derivative resulting in - 2*sin(x^2) - 4*x^2*cos(x^2), then I want to evaluate this second derivative at x = 0 to x = 1, and display the maximum value of the populated values.
I have:
syms x
f = cos(x^2);
secondD = diff(diff(f));
for i = 0:1
y = max(secondD(i))
end
Can someone help?
You can do it easily by subs and double:
syms x
f = cos(x^2);
secondD = diff(diff(f));
% instead of the for loop
epsilon = 0.01;
specified_range = 0:epsilon:1;
[max_val, max_ind] = max(double(subs(secondD, specified_range)));
Please note that it is a numerical approach to find the maximum and the returned answer is not completely correct all the time. However, by increasing the epsilon, you can expect a better result in general (again in some cases it is not completely correct).

Taylor series for (exp(x) - exp(-x))/(2*x)

I've been asked to write a function that calculates the Taylor series for (exp(x) - exp(-x))/(2*x) until the absolute error is smaller than the eps of the machine.
function k = tayser(xo)
f = #(x) (exp(x) - exp(-x))/(2*x);
abserror = 1;
sum = 1;
n=2;
while abserror > eps
sum = sum + (xo^n)/(factorial(n+1));
n=n+2;
abserror = abs(sum-f(xo));
disp(abserror);
end
k=sum;
My issue is that the abserror never goes below the eps of the machine which results to an infinite loop.
The problem is expression you're using. For small numbers exp(x) and exp(-x) are approximately equal, so exp(x)-exp(-x) is close to zero and definitely below 1. Since you start with 1 and only add positive numbers, you'll never reach the function value.
Rewriting the expression as
f = #(x) sinh(x)/x;
will work, because it's more stable for these small values.
You can also see this by plotting both functions:
x = -1e-14:1e-18:1e-14;
plot(x,(exp(x) - exp(-x))./(2*x),x,sinh(x)./x)
legend('(exp(x) - exp(-x))/(2*x)','sinh(x)/x')
gives

Matlab is rounding my sigmoid function

I have implemented a sigmoid function as follows in Matlab.
function [y] = sig(x)
y = 1.0 / (1.0 + exp(-x));
end
When I give it a large input such as 100, the function rounds off my result and gives me a 1.
How can I get its accurate value? Is it possible or am I limited to a low range for the value of x.
If you want the difference between 1 and your sigmoid function, you could define a function with the simplified mathematical expression:
1 - 1/(1+exp(-x)) = (1+exp(-x))/(1+exp(-x)) - 1/(1+exp(-x)) = exp(-x) / (1+exp(-x))
function [y] = one_minus_sig(x)
y = exp(-x) / (1+exp(-x));
end
And then:
one_minus_sig(100) = 3.7200759760208356e-44
1.0000000000000... is accurate to about 44 digits so I'm not sure what the problem would be?
Edit: In an earlier version, I said 300 digits - for some reason I had used x=900 in that computation. Here are a few digits:
0.99999999999999999999999999999999999999999996279924023979164037040304196136881662641107846012870706553139188204855222012652822147634307
Computed using Maple.

solve In Matlab a quadratic equation with very small coefficients

I'm implementing a code in matlab to solve quadratic equations, using the resolvent formula:
Here´s the code:
clear all
format short
a=1; b=30000000.001; c=1/4;
rdelta=sqrt(b^2-4*a*c);
x1=(-b+rdelta)/(2*a);
x2=(-b-rdelta)/(2*a);
fprintf(' Roots of the polynomial %5.3f x^2 + %5.3f x+%5.3f \n',a,b,c)
fprintf ('x1= %e\n',x1)
fprintf ('x2= %e\n\n',x2)
valor_real_x1= -8.3333e-009;
valor_real_x2= -2.6844e+007;
error_abs_x1 = abs (valor_real_x1-x1);
error_abs_x2 = abs (valor_real_x2-x2);
error_rel_x1 = abs (error_abs_x1/valor_real_x1);
error_rel_x2 = abs (error_abs_x2/valor_real_x2);
fprintf(' absolute_errorx1 = |real value - obtained value| = |%e - %e| = %e \n',valor_real_x1,x1,error_abs_x1)
fprintf(' absolute_errorx2 = |real value - obtained value| = |%e - %e| = %e \n\n',valor_real_x2,x2,error_abs_x2)
fprintf(' relative error_x1 = |absolut error / real value| = |%e / %e| = %e \n',error_abs_x1,valor_real_x1,error_rel_x1 )
fprintf(' relative_error_x2 = |absolut error / real value| = |%e / %e| = %e \n',error_abs_x2,valor_real_x2,error_rel_x2)
The problem I have is that it gives me an exact solution, ie for values ​​a = 1, b = 30000000,001 c = 1/4, the values ​​of the roots are:
Roots of the polynomial 1.000 x^2 + 30000000.001 x+0.250
x1= -9.313226e-009
x2= -3.000000e+007
Knowing that the exact value of the roots of the polynomial are:
x1= -8.3333e-009
x2= -2.6844e+007
Which gives me the following errors in the absolute and relative precision of the calculations:
absolute_errorx1 = |real value - obtained value| = |-8.333300e-009 - -9.313226e-009| = 9.799257e-010
absolute_errorx2 = |real value - obtained value| = |-2.684400e+007 - -3.000000e+007| = 3.156000e+006
relative error_x1 = |absolut error / real value| = |9.799257e-010 / -8.333300e-009| = 1.175916e-001
relative_error_x2 = |absolut error / real value| = |3.156000e+006 / -2.684400e+007| = 1.175682e-001
My question is: Is there an optimum method to obtain the roots of a quadratic equation?, ie I can make changes to my code to reduce the relative error between the expected solution and the resulting solution?
Using the quadratic formula directly in this cases results in a large loss of numerical precision from subtracting two values of very similar magnitude. This is because the expression
sqrt(b*b - 4*a*c)
is nearly the same as b. So you should use only one of these two roots, the one that does not involve subtracting two very close values, and for the other root you can use (for instance) the fact that the product of roots of a quadratic is c/a. I'll let you fill in the gaps.
Why does this sound like a homework problem from a first class in numerical analysis?
It has been a while since I was that young, but as I recall there is a trick. Anyway, you are wrong. The true roots of that polynomial are
solve('x^2 + 30000000.001*x + 0.25')
ans =
-30000000.000999991666666666944442
-0.0000000083333333330555578703796293981491
How well does roots do here?
p = [1 30000000.001 1/4];
format long g
roots(p)
ans =
-30000000.001
-8.33333333305556e-09
That actually seems pretty good. How does HPF do?
DefaultNumberOfDigits 64
a = hpf(1);
b = hpf('30000000.001');
c = hpf('0.25');
r1 = (-b + sqrt(b*b - 4*a*c))/(2*a)
r1 =
-0.000000008333333333055557870379629398149125529835186899898569329967
r2 = (-b - sqrt(b*b - 4*a*c))/(2*a)
r2 =
-30000000.000999991666666666944442129620370601850874470164813100101
Yep, HPF works nicely enough too.
So what happens when you use double precision numbers and the standard formula? Yeah, crapola arrives.
a = 1;
b = 30000000.001;
c = 0.25;
>> r1 = (-b + sqrt(b*b - 4*a*c))/(2*a)
r1 =
-7.45058059692383e-09
>> r2 = (-b - sqrt(b*b - 4*a*c))/(2*a)
r2 =
-30000000.001
Again, massive subtractive cancellation eats away at the result. (I seem to recall that was the problem you had in your last question.)
There is a trick you can use. See that the large solution was well estimated, just not the one near zero. So, what happens if you solved for the roots of fliplr(p) using the quadratic formula? How does this solve your problem? What transformation is implicitly done when you do that? (Sorry, but I won't do your homework. I think the above was enough of a hint anyway.)
i think your "real" values might be wrong (or maybe it's a precision thing... I dunno)
a*(valor_real_x1^2)+b*(valor_real_x1)+c
ans =
9.9999e-07
a*(valor_real_x2^2)+b*(valor_real_x2)+c
ans =
-8.4720e+13
A nice formula for this problem:
var q = sqrt(c*a)/b;
var f = .5 + .5 *sqrt(1-4*q*q);
var x1=-b*f/a;
var x2=-c/(f*b);