Using MATLAB's symbolic engine to solve a 4 by 4 linear system - matlab

I have what seems to me to be a very simple symbolic math problem. I have a linear system of 4 equations and 4 unknowns. The coefficients are non-numerical constants. I coded the problem up in MATLAB. My code is below. It ran for a couple of hours before I shut it down. To me, I should be getting an answer within minutes. I'm not sure what the problem is.
syms a b c d e f g h k l m n o p q r W X Y Z A B
eqn1=a*W+b*X+c*Y+d*Z==A;
eqn2=e*W+f*X+g*Y+h*Z==B;
eqn3=k*W+l*X+m*Y+n*Z==0;
eqn4=o*W+p*X+q*Y+r*Z==0;
Soln=solve([eqn1,eqn2,eqn3,eqn4],[W,X,Y,Z],'ReturnConditions',true);
SolnW=Soln.W
SolnX=Soln.X
SolnY=Soln.Y
SolnZ=Soln.Z
Conditions=Soln.conditions
Parameters=Soln.parameters
I have two questions.
(1) Is the way I have approached the problem efficient? For example, is perhaps MATHEMATICA or MAPLE more suited to the job?
(2) I anticipated a Cramer's-like solution with terms representing expanded forms of determinants. Of course, this is going to be ugly. Is there a way to get MATLAB to simplify the result algebraically?

With matlab and linear systems you should take a different approach, working with matrix like this, matlab really likes working with matrix, so this is the way that you should work using matlab. then your code is pretty fast:
>> syms a b c d e f g h k l m n o p q r W X Y Z A B
>> eqn1=a*W+b*X+c*Y+d*Z==A;
eqn2=e*W+f*X+g*Y+h*Z==B;
eqn3=k*W+l*X+m*Y+n*Z==0;
eqn4=o*W+p*X+q*Y+r*Z==0;
>> [A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [W, X, Y,Z])
A =
[ a, b, c, d]
[ e, f, g, h]
[ k, l, m, n]
[ o, p, q, r]
B =
A
B
0
0
>> linsolve(A,B)
ans =
-(B*b*m*r - B*b*n*q - B*c*l*r + B*c*n*p + B*d*l*q - B*d*m*p - A*f*m*r + A*f*n*q + A*g*l*r - A*g*n*p - A*h*l*q + A*h*m*p)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
(B*a*m*r - B*a*n*q - B*c*k*r + B*c*n*o + B*d*k*q - B*d*m*o - A*e*m*r + A*e*n*q + A*g*k*r - A*g*n*o - A*h*k*q + A*h*m*o)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
-(B*a*l*r - B*a*n*p - B*b*k*r + B*b*n*o + B*d*k*p - B*d*l*o - A*e*l*r + A*e*n*p + A*f*k*r - A*f*n*o - A*h*k*p + A*h*l*o)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
(B*a*l*q - B*a*m*p - B*b*k*q + B*b*m*o + B*c*k*p - B*c*l*o - A*e*l*q + A*e*m*p + A*f*k*q - A*f*m*o - A*g*k*p + A*g*l*o)/(a*f*m*r - a*f*n*q - a*g*l*r + a*g*n*p + a*h*l*q - a*h*m*p - b*e*m*r + b*e*n*q + b*g*k*r - b*g*n*o - b*h*k*q + b*h*m*o + c*e*l*r - c*e*n*p - c*f*k*r + c*f*n*o + c*h*k*p - c*h*l*o - d*e*l*q + d*e*m*p + d*f*k*q - d*f*m*o - d*g*k*p + d*g*l*o)
I hope this helps. this is a very general answer, so you shoul limit the posible values with symbolic assumption

Related

Error while solving system of non-linear equations in MATLAB

I am trying to solve system of non-linear equation simultaneously on MATLAB. I am getting the error:
Comma separated list expansion has cell syntax for an array that is not a cell".
The screenshot of the error is attached:
My code is given below:
syms x y z T
x1=1;
x2=1;
x3=1;
x4=1;
y1=1;
y2=1;
y3=1;
y4=1;
z1=1;
z2=1;
z3=1;
z4=1;
c=1;
t1=1;
t21=1;
t31=1;
t41=1;
eq1 = ((x1 - x)^2 + (y1 - y)^2 + (z1 - z)^2 )^1/2 - (c * t1) + (c * T)==0;
eq2 = ((x2 - x)^2 + (y2 - y)^2 + (z2 - z)^2 )^1/2 - (c * t21) - (c*t1) + (c*T)==0;
eq3 = ((x3 - x)^2 + (y3 - y)^2 + (z3 - z)^2 )^1/2 - (c * t31) - (c*t1) + (c*T)==0;
eq4 = ((x4 - x)^2 + (y4 - y)^2 + (z4 - z)^2 )^1/2 - (c * t41) - (c*t1) + (c*T)==0;
sol = solve(eq1, eq2, eq3, eq4);
xSol = sol.x
ySol = sol.y
zSol = sol.z
TSol = sol.T
When I run your code, there is no error in my MATLAB 2020a.
However it returns an empty solution. This is normal, your equations are not solvable. Check eq1 and eq2. For your values, they are:
eq1 = ((1 - x)^2 + (1- y)^2 + (1 - z)^2 )^1/2 - (1) + (1 * T)==0;
eq2 = ((1 - x)^2 + (1- y)^2 + (1 - z)^2 )^1/2 - (2) + (1 * T)==0;
This has of course no solution,as you are basically saying:
eq1= ((1 - x)^2 + (1- y)^2 + (1 - z)^2 )^1/2 + (1 * T)==1
eq2= ((1 - x)^2 + (1- y)^2 + (1 - z)^2 )^1/2 + (1 * T)==2
Both can not coexist.
It is possible that you are using an older version of MATLAB that throws warning when solution does not exist and does not allow you to read it later, where your error happens.

MATLAB: convert from euler (complex fourier) to sinus function (bn coefficients)

I have the following script
clc; clear all; close all;
syms x n
f = x;
L = 1;
subplot(2,1,1)
h = ezplot(f,[-L,L])
set(h, 'Color','r','LineWidth',1)
a0 = (1/L) * int(f * cos(0* pi*x/L),-L,L)
an = (1/L) * int(f * cos(n* pi*x/L),-L,L)
bn = (1/L)* int(f* sin(n* pi*x/L),-L,L)
fx = a0/2 + symsum((an* cos(n*pi*x/L) + bn* sin(n*pi*x/L)),n,1,5)
% for n =5, the answer: fx = (2*sin(pi*x))/pi - sin(2*pi*x)/pi +
%(2*sin(3*pi*x))/(3*pi) - sin(4*pi*x)/(2*pi) + (2*sin(5*pi*x))/(5*pi)
hold on
h = ezplot(fx,[-L,L])
grid on
%Solution with complex Fourier
c0 = (1/(2*L))*int(f*exp(-j*0*pi*x/L),-L,L)
cn = (1/(2*L))*int(f*exp(-j*n*pi*x/L),-L,L)
subplot(2,1,2)
h = ezplot(f,[-L,L])
set(h, 'Color','r','LineWidth',1)
fx_c = c0 + symsum(cn*exp(j*n*pi*x/L),n,-5,-1) + ...
symsum(cn*exp(j*n*pi*x/L),n,1,5) % n for complex -5,5
hold on
h = ezplot(fx_c,[-L,L])
grid on
My question: Since the answer of fx should be equal to fx_c (complex fourier). We can see from the figures produced by these 2 functions. They are same. But
fx =
(2*sin(pi*x))/pi - sin(2*pi*x)/pi + (2*sin(3*pi*x))/(3*pi) - sin(4*pi*x)/(2*pi) + (2*sin(5*pi*x))/(5*pi)
and
fx_c =
exp(-pi*x*i)*((pi*i - 1)/(2*pi^2) + (pi*i + 1)/(2*pi^2)) - exp(pi*x*i)*((pi*i - 1)/(2*pi^2) + (pi*i + 1)/(2*pi^2)) - exp(-pi*x*2*i)*((pi*2*i - 1)/(8*pi^2) + (pi*2*i + 1)/(8*pi^2)) + exp(pi*x*2*i)*((pi*2*i - 1)/(8*pi^2) + (pi*2*i + 1)/(8*pi^2)) + exp(-pi*x*3*i)*((pi*3*i - 1)/(18*pi^2) + (pi*3*i + 1)/(18*pi^2)) - exp(pi*x*3*i)*((pi*3*i - 1)/(18*pi^2) + (pi*3*i + 1)/(18*pi^2)) - exp(-pi*x*4*i)*((pi*4*i - 1)/(32*pi^2) + (pi*4*i + 1)/(32*pi^2)) + exp(pi*x*4*i)*((pi*4*i - 1)/(32*pi^2) + (pi*4*i + 1)/(32*pi^2)) + exp(-pi*x*5*i)*((pi*5*i - 1)/(50*pi^2) + (pi*5*i + 1)/(50*pi^2)) - exp(pi*x*5*i)*((pi*5*i - 1)/(50*pi^2) + (pi*5*i + 1)/(50*pi^2))
How to convert fx_c to be fx?
They are related by Euler's formula. You can check it with rewrite command:
>> rewrite(exp(1i*x), 'cos')
ans =
cos(x) + sin(x)*1i
Applying it to your function and simplifying a bit, you can get to the same expression:
>> expand(rewrite(fx_c, 'cos'), 'ArithmeticOnly', true)
ans =
(2*sin(pi*x))/pi - sin(2*pi*x)/pi + (2*sin(3*pi*x))/(3*pi) - sin(4*pi*x)/(2*pi) + (2*sin(5*pi*x))/(5*pi)
>> fx
fx =
(2*sin(pi*x))/pi - sin(2*pi*x)/pi + (2*sin(3*pi*x))/(3*pi) - sin(4*pi*x)/(2*pi) + (2*sin(5*pi*x))/(5*pi)

Solving Differential equations with 7 unknowns

I want to solve the 7 differential equations which are functions of time for the 7 unknowns.
I wanted to find the solutions of the equations:
eo(t)=f1(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
e1(t)=f2(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
e2(t)=f3(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
e3(t)=f4(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
w1(t)=f5(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
w2(t)=f6(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
w3(t)=f7(e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
I have generated the equations of e0, e1, e2, e3, w1, w2, w3.
Now, how do I solve these equations, and which commands are needed?
I need to find the values of e0, e1, e2, e3, w1, w2, w3 and get the numerical value of these with respect to t.
The equations which have to be solved are
e0 = - (e_1(t)*w_1(t))/2 - (e_2(t)*w_2(t))/2 - (e_3(t)*w_3(t))/2
e1 = (e_0(t)*w_1(t))/2 - (e_2(t)*w_3(t))/2 - (e_3(t)*w_2(t))/2
e2 =(e_0(t)*w_2(t))/2 - (e_1(t)*w_3(t))/2 + (e_3(t)*w_1(t))/2
e3 = (e_0(t)*w_3(t))/2 + (e_1(t)*w_2(t))/2 - (e_2(t)*w_1(t))/2
w1 = w_2(t)*(1.98019*e_3(t)*(e_1(t)*w_1(t) + e_2(t)*w_2(t) + e_3(t)*w_3(t)) + 1.980*e_0(t)*(e_0(t)*w_3(t) + e_1(t)*w_2(t) - 1.0*e_2(t)*w_1(t)) - 1.980*e_1(t)*(e_0(t)*w_2(t) - 1.0*e_1(t)*w_3(t) + e_3(t)*w_1(t)) - 1.9801*e_2(t)*(e_2(t)*w_3(t) - 1.0*e_0(t)*w_1(t) + e_3(t)*w_2(t))) - 1.0*w_1(t)*(1.0*e_0(t)*(e_1(t)*w_1(t) + e_2(t)*w_2(t) + e_3(t)*w_3(t)) + 1.0*e_1(t)*(e_2(t)*w_3(t) - 1.0*e_0(t)*w_1(t) + e_3(t)*w_2(t)) - 1.0*e_2(t)*(e_0(t)*w_2(t) - 1.0*e_1(t)*w_3(t) + e_3(t)*w_1(t)) - 1.0*e_3(t)*(e_0(t)*w_3(t) + e_1(t)*w_2(t) - 1.0*e_2(t)*w_1(t))) - 1.0*w_3(t)*(1.0*e_0(t)*(e_0(t)*w_2(t) - 1.0*e_1(t)*w_3(t) + e_3(t)*w_1(t)) - 1.0*e_2(t)*(e_1(t)*w_1(t) + e_2(t)*w_2(t) + e_3(t)*w_3(t)) + 1.0*e_1(t)*(e_0(t)*w_3(t) + e_1(t)*w_2(t) - 1.0*e_2(t)*w_1(t)) + 1.0*e_3(t)*(e_2(t)*w_3(t) - 1.0*e_0(t)*w_1(t) + e_3(t)*w_2(t))) - (63.366*kappa^2*(0.72470*w_1(t) + 0.355*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 1.0*e_3(t)^2)*(e_0(t)^2 - 1.0*e_1(t)^2 + e_2(t)^2 - 1.0*e_3(t)^2) - 1.0*(2.0*e_0(t)*e_3(t) + 2.0*e_1(t)e_2(t))(2.0*e_0(t)*e_3(t) - 2.0*e_1(t)*e_2(t))) - 0.724*w_3(t) + 0.362*kappa*((e_0(t)^2 + e_1(t)^2 - 1.0*e_2(t)^2 - 1.0*e_3(t)^2)*(e_0(t)^2 - 1.0*e_1(t)^2 + e_2(t)^2 - 1.0*e_3(t)^2) + (2.0*e_0(t)*e_3(t) + 2.0*e_1(t)e_2(t))(2.0*e_0(t)*e_3(t) - 2.0*e_1(t)*e_2(t)))))/(l^5*rho)
I used this code in MATLAB after assigning the values
soll=ode45(e0,e1,e2,e3,w1,w2,w3,e_0(t),e_1(t),e_2(t),e_3(t),w_1(t),w_2(t),w_3(t))
But I got the following error message:
Undefined function 'exist' for input arguments of type 'sym'.
Error in odearguments (line 59) if (exist(ode)==2)
Error in ode45 (line 113) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Please enlighten me where I have gone wrong.
This error means that ode45 cannot solve symbolic equations (your variables are of type 'sym'). In fact ode45 is a numerical solver which works on functions, not on symbolical expressions. Here's how to define a function in Matlab:
First, you need to create a new m-file then type this code
function y = f(x)
y = 2 * (x^3) + 7 * (x^2) + x;
Save with filename 'f.m'

Matlab : sum of functions by Iteration?

I am trying to make a code to produce sum of functions through iteration.
syms c p
for j=1:10
func=#(c,p)(j+1-c-p*j)^2
yfunc=#(c,p)(yfunc(c,p)+func(c,p))
end
At the end of the day, 'yfunc' will have 10 different terms from 'func's.
But this code doesn't work.
Could you help me?
The following works:
syms c p;
yfunc = symfun(0,[c,p]);
for j=1:10
func= symfun((j+1-c-p*j)^2,[c,p]);
yfunc=yfunc+func;
end
This results in:
yfunc(c, p) =
(c + p - 2)^2 + (c + 2*p - 3)^2 + (c + 3*p - 4)^2 + (c + 4*p - 5)^2 + (c + 5*p - 6)^2 + (c + 6*p - 7)^2 + (c + 7*p - 8)^2 + (c + 8*p - 9)^2 + (c + 9*p - 10)^2 + (c + 10*p - 11)^2

using quad to integrate function with respect to just one variable

is there any way i can integrate function of two variable, say
f=#(x) x^2 + x*y
over just x
tried quad(f, a, b)
but doesn't work, looking for alternative solution
Looks like you want something like this:
y = 100; % whatever y is
a = 0;
b = 2;
% you'll need to vectorize the integrand function
f = #(x) x.*x + x.*y
val = quad(f, a, b);
However, if you are looking for an algebraic answer, you'll need to use the Symbolic Toolbox, or some other software, or your calculus book. :-)
The whole "vectorize" thing comes from the Mathworks quad documentation that says:
The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x.
Sorry, but quad does not solve symbolic problems. It does only numerical integration.
syms x y
int(x^2 + x*y,x)
ans =
(x^2*(2*x + 3*y))/6
The natural way to solve a symbolic problem is to use a symbolic tool.
From the followup, Anya wants something in-between. To steal the words of an old rock star named Mick, "You can't always get what you want."
Again, quad can't be used if you wish to integrate ONLY over x, as quad is an adaptive tool.
In SOME SIMPLE cases, you can use a simple tool like Simpson's rule to do the work. For example, suppose you wanted to solve the above problem, with x integrated over the interval [0 1]. For purposes of comparison, I'll do it symbolically first.
syms x y
res = int(x^2 + x*y,x);
subs(res,x,1) - subs(res,0)
ans =
y/2 + 1/3
Now, lets try it using a numerical integration on x.
syms y
x = 0:.01:1;
coef = mod((0:100)',2)*2 + 2;
coef([1 end]) = 1;
coef = 0.01*coef/3;
(x.^2 + x.*y)*coef
ans =
y/2 + 1/3
So in this very SIMPLE case, it did work. How about something a little more complicated? Integrate x*exp(x*y) over the interval [-1 1]. Again, a known form is accessible symbolically.
syms x y
res = int(x*exp(x*y),x);
res = subs(res,x,1) - subs(res,-1)
res =
(exp(-y)*(y + 1))/y^2 + (exp(y)*(y - 1))/y^2
To test it out later, what value does this take on at y = 1/2?
vpa(subs(res,y,1/2))
ans =
0.34174141687554424792549563431876
Lets try the same trick, using Simpson's rule.
syms y
x = -1:.01:1;
coef = mod((-100:100)',2)*2 + 2;
coef([1 end]) = 1;
coef = 0.01*coef/3;
res = (x.*exp(x*y))*coef
res =
exp(y/2)/300 - exp(-y/2)/300 - exp(-y)/300 - exp(-y/4)/300 + exp(y/4)/300 - exp(-y/5)/750 + exp(y/5)/750 - exp(-(3*y)/4)/100 - exp(-(2*y)/5)/375 + exp((2*y)/5)/375 + exp((3*y)/4)/100 - exp(-(3*y)/5)/250 + exp((3*y)/5)/250 - (2*exp(-(4*y)/5))/375 + (2*exp((4*y)/5))/375 - exp(-y/10)/1500 + exp(y/10)/1500 - exp(-(3*y)/10)/500 + exp((3*y)/10)/500 - (7*exp(-(7*y)/10))/1500 + (7*exp((7*y)/10))/1500 - (3*exp(-(9*y)/10))/500 + (3*exp((9*y)/10))/500 - exp(-y/20)/1500 + exp(y/20)/1500 - exp(-(3*y)/20)/500 + exp((3*y)/20)/500 - exp(-y/25)/3750 + exp(y/25)/3750 - (7*exp(-(7*y)/20))/1500 - exp(-(2*y)/25)/1875 + exp((2*y)/25)/1875 + (7*exp((7*y)/20))/1500 - exp(-(3*y)/25)/1250 + exp((3*y)/25)/1250 - (3*exp(-(9*y)/20))/500 - (2*exp(-(4*y)/25))/1875 + (2*exp((4*y)/25))/1875 + (3*exp((9*y)/20))/500 - (11*exp(-(11*y)/20))/1500 - exp(-(6*y)/25)/625 + exp((6*y)/25)/625 + (11*exp((11*y)/20))/1500 - (7*exp(-(7*y)/25))/3750 + (7*exp((7*y)/25))/3750 - (13*exp(-(13*y)/20))/1500 - (4*exp(-(8*y)/25))/1875 + (4*exp((8*y)/25))/1875 + (13*exp((13*y)/20))/1500 - (3*exp(-(9*y)/25))/1250 + (3*exp((9*y)/25))/1250 - (11*exp(-(11*y)/25))/3750 + (11*exp((11*y)/25))/3750 - (17*exp(-(17*y)/20))/1500 - (2*exp(-(12*y)/25))/625 + (2*exp((12*y)/25))/625 + (17*exp((17*y)/20))/1500 - (13*exp(-(13*y)/25))/3750 + (13*exp((13*y)/25))/3750 - (19*exp(-(19*y)/20))/1500 - (7*exp(-(14*y)/25))/1875 + (7*exp((14*y)/25))/1875 + (19*exp((19*y)/20))/1500 - (8*exp(-(16*y)/25))/1875 + (8*exp((16*y)/25))/1875 - (17*exp(-(17*y)/25))/3750 + (17*exp((17*y)/25))/3750 - (3*exp(-(18*y)/25))/625 + (3*exp((18*y)/25))/625 - (19*exp(-(19*y)/25))/3750 + (19*exp((19*y)/25))/3750 - (7*exp(-(21*y)/25))/1250 + (7*exp((21*y)/25))/1250 - (11*exp(-(22*y)/25))/1875 + (11*exp((22*y)/25))/1875 - (23*exp(-(23*y)/25))/3750 + (23*exp((23*y)/25))/3750 - (4*exp(-(24*y)/25))/625 + (4*exp((24*y)/25))/625 - exp(-y/50)/7500 + exp(y/50)/7500 - exp(-(3*y)/50)/2500 + exp((3*y)/50)/2500 - (7*exp(-(7*y)/50))/7500 + (7*exp((7*y)/50))/7500 - (3*exp(-(9*y)/50))/2500 + (3*exp((9*y)/50))/2500 - (11*exp(-(11*y)/50))/7500 + (11*exp((11*y)/50))/7500 - (13*exp(-(13*y)/50))/7500 + (13*exp((13*y)/50))/7500 - (17*exp(-(17*y)/50))/7500 + (17*exp((17*y)/50))/7500 - (19*exp(-(19*y)/50))/7500 + (19*exp((19*y)/50))/7500 - (7*exp(-(21*y)/50))/2500 + (7*exp((21*y)/50))/2500 - (23*exp(-(23*y)/50))/7500 + (23*exp((23*y)/50))/7500 - (9*exp(-(27*y)/50))/2500 + (9*exp((27*y)/50))/2500 - (29*exp(-(29*y)/50))/7500 + (29*exp((29*y)/50))/7500 - (31*exp(-(31*y)/50))/7500 + (31*exp((31*y)/50))/7500 - (11*exp(-(33*y)/50))/2500 + (11*exp((33*y)/50))/2500 - (37*exp(-(37*y)/50))/7500 + (37*exp((37*y)/50))/7500 - (13*exp(-(39*y)/50))/2500 + (13*exp((39*y)/50))/2500 - (41*exp(-(41*y)/50))/7500 + (41*exp((41*y)/50))/7500 - (43*exp(-(43*y)/50))/7500 + (43*exp((43*y)/50))/7500 - (47*exp(-(47*y)/50))/7500 + (47*exp((47*y)/50))/7500 - (49*exp(-(49*y)/50))/7500 + (49*exp((49*y)/50))/7500 - exp(-y/100)/7500 + exp(y/100)/7500 - exp(-(3*y)/100)/2500 + exp((3*y)/100)/2500 - (7*exp(-(7*y)/100))/7500 + (7*exp((7*y)/100))/7500 - (3*exp(-(9*y)/100))/2500 + (3*exp((9*y)/100))/2500 - (11*exp(-(11*y)/100))/7500 + (11*exp((11*y)/100))/7500 - (13*exp(-(13*y)/100))/7500 + (13*exp((13*y)/100))/7500 - (17*exp(-(17*y)/100))/7500 + (17*exp((17*y)/100))/7500 - (19*exp(-(19*y)/100))/7500 + (19*exp((19*y)/100))/7500 - (7*exp(-(21*y)/100))/2500 + (7*exp((21*y)/100))/2500 - (23*exp(-(23*y)/100))/7500 + (23*exp((23*y)/100))/7500 - (9*exp(-(27*y)/100))/2500 + (9*exp((27*y)/100))/2500 - (29*exp(-(29*y)/100))/7500 + (29*exp((29*y)/100))/7500 - (31*exp(-(31*y)/100))/7500 + (31*exp((31*y)/100))/7500 - (11*exp(-(33*y)/100))/2500 + (11*exp((33*y)/100))/2500 - (37*exp(-(37*y)/100))/7500 + (37*exp((37*y)/100))/7500 - (13*exp(-(39*y)/100))/2500 + (13*exp((39*y)/100))/2500 - (41*exp(-(41*y)/100))/7500 + (41*exp((41*y)/100))/7500 - (43*exp(-(43*y)/100))/7500 + (43*exp((43*y)/100))/7500 - (47*exp(-(47*y)/100))/7500 + (47*exp((47*y)/100))/7500 - (49*exp(-(49*y)/100))/7500 + (49*exp((49*y)/100))/7500 - (17*exp(-(51*y)/100))/2500 + (17*exp((51*y)/100))/2500 - (53*exp(-(53*y)/100))/7500 + (53*exp((53*y)/100))/7500 - (19*exp(-(57*y)/100))/2500 + (19*exp((57*y)/100))/2500 - (59*exp(-(59*y)/100))/7500 + (59*exp((59*y)/100))/7500 - (61*exp(-(61*y)/100))/7500 + (61*exp((61*y)/100))/7500 - (21*exp(-(63*y)/100))/2500 + (21*exp((63*y)/100))/2500 - (67*exp(-(67*y)/100))/7500 + (67*exp((67*y)/100))/7500 - (23*exp(-(69*y)/100))/2500 + (23*exp((69*y)/100))/2500 - (71*exp(-(71*y)/100))/7500 + (71*exp((71*y)/100))/7500 - (73*exp(-(73*y)/100))/7500 + (73*exp((73*y)/100))/7500 - (77*exp(-(77*y)/100))/7500 + (77*exp((77*y)/100))/7500 - (79*exp(-(79*y)/100))/7500 + (79*exp((79*y)/100))/7500 - (27*exp(-(81*y)/100))/2500 + (27*exp((81*y)/100))/2500 - (83*exp(-(83*y)/100))/7500 + (83*exp((83*y)/100))/7500 - (29*exp(-(87*y)/100))/2500 + (29*exp((87*y)/100))/2500 - (89*exp(-(89*y)/100))/7500 + (89*exp((89*y)/100))/7500 - (91*exp(-(91*y)/100))/7500 + (91*exp((91*y)/100))/7500 - (31*exp(-(93*y)/100))/2500 + (31*exp((93*y)/100))/2500 - (97*exp(-(97*y)/100))/7500 + (97*exp((97*y)/100))/7500 - (33*exp(-(99*y)/100))/2500 + (33*exp((99*y)/100))/2500 + exp(y)/300
So I got a result, but its not the analytical one I wanted, and a bit of a nasty mess. Is it correct?
vpa(subs(res,y,1/2))
ans =
0.34174141693463006644516447861307
I'll copy the analytical result from above so we can compare...
0.34174141687554424792549563431876
As you can see, Simpson's rule, at a step size of 0.01 over [-1,1], did reasonably well, agreeing out to about 9 decimal digits.
There is no assurance that this technique will work as well on any more general kernel, but it might give you what you desire.