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.
Related
I've been trying to calculate and plot trajectories for orbital motion, but got stuck with the variable types.
All input variables are of double type, as well as the desired output. As a consequence, variables of orbital system (the ones with "_orb" in the names) are of double type.
Here's the script. The diffficulties start from "Velocity corrections" part, in which I want to calculate minimum for a function of velocity change:
function trajectory_final (x0, y0 , z0, vx0, vy0, vz0, q0, q1, q2, q3)
o = 0.00114;
xk = 0;
yk = -150;
zk = 150;
double t;
%% Coordinate system transition:
x0_orb = (1 - 2*q2*q2 - 2*q3*q3)*x0 + 2*(q1*q2 + q0*q3)*y0 + 2*(q1*q3 - q0*q2)*z0;
y0_orb = 2*(q1*q2 - q0*q3)*x0 + (1 - 2*q1*q1 - 2*q3*q3)*y0 + 2*(q2*q3 + q0*q1)*z0;
z0_orb = 2*(q1*q3 + q0*q2)*x0 + 2*(q2*q3 - q0*q1)*y0 + (1 - 2*q1*q1 - 2*q2*q2)*z0;
vx0_orb = (1 - 2*q2*q2 - 2*q3*q3)*vx0 + 2*(q1*q2 + q0*q3)*vy0 + 2*(q1*q3 - q0*q2)*vz0;
vy0_orb = 2*(q1*q2 - q0*q3)*vx0 + (1 - 2*q1*q1 - 2*q3*q3)*vy0 + 2*(q2*q3 + q0*q1)*vz0;
vz0_orb = 2*(q1*q3 + q0*q2)*vx0 + 2*(q2*q3 - q0*q1)*vy0 + (1 - 2*q1*q1 - 2*q2*q2)*vz0;
%% Velocity corrections:
vx1 = sym('(xk - x0_orb - (6*o*t - 6*sin(o*t))*y0_orb - (-2*cos(o*t)/o + 2/o)*((-3*cos(o*t) + 4)*y0_orb - yk))/(4*sin(o*t)/o - 3*t - (2*cos(o*t)/o - 2/o)*(2*cos(o*t)/o - 2/o))');
vy1 = sym('(-3*cos(o*t) + 4)*y0_orb + (2*cos(o*t)/o - 2/o)*vx1 - yk');
vz1 = sym('(zk - (cos(o*t))*z0_orb)/(sin(o*t)/o)');
dvx = vx1 - vx0_orb;
dvy = vy1 - vy0_orb;
dvz = vz1 - vz0_orb;
total = sqrt((dvx)^2 + (dvy)^2 + (dvz)^2);
D = diff(total);
D_math = matlabFunction(D);
root = fzero(D_math, 600);
However, several problems occur:
1) If I run it as is now, variables in vx1, vy1 and vz1 are not replaced with their values assigned at launch, resulting in "Not enough input arguments" for fzero.
2) If I make vx1, vy1 and vz1 numerical, the error "Undefined function or variable t" appears;
3) If I make vx1, vy1 and vz1 numerical AND assign boundaries for t, then I cannot find root for D_math function on the desired interval.
Could you please tell me what is the best way to handle this problem?
I'm have a system of two questions with two unknown variables that I'm trying to use Matlab's solve() function to solve, but hitting errors that are almost surely on my end. I'm trying to solve The following two equations where r, lambda, and X are constants. To do so, I've been trying to run the following:
clear all;
syms VA VB r X L;
assumeAlso(VB <= VA);
assumeAlso(0 <= VB);
assumeAlso(0 < r <= 1);
assumeAlso(0 < L);
assumeAlso(0 < X);
assumeAlso(VA, 'real');
assumeAlso(VB, 'real');
assumeAlso(r, 'real');
assumeAlso(L, 'real');
assumeAlso(X, 'real');
eqns = [VA == ((r*VA + L*(VA-VB))^2)/(2*X*(r+2*L)) + (L*(VA+VB)*(r*VA+L*(VA-VB)))/(X*(r+2*L)) + ...
(((r^2/L+r)*VA + r*VB)^2 - (r*VA+L*(VA-VB))^2)/(2*X*(r+L)) + ...
(L*VB*((r+L)*VB - L*VA + r^2/L*VA))/(X*(r+L)) + ...
(X/(2*r)) - (((r^2/L+r)*VA+r*VB)^2)/(2*X*r) - VA*(1-1/X*((r^2/L+r)*VA+r*VB)), ...
...
VB == (L^2*VA^2 - L^2*VB^2 + r*VA*(L*VA+L*VB))/(X*(r+2*L)) + ...
(L*VA*(r^2/L*VA + r*VB - L*(VA-VB)))/(X*(r+L)) + ...
(VA*(X-((r^2/L+r)*VA + r*VB)))/X];
S = solve(eqns,[VA VB])
My goal is to solve for VA and VB in terms of r, X, and L, which seems like it should be possible. When I fill in values for r, L and X I am easily able to obtain a solution.
When I run this code, however, I get the message:
Warning: Explicit solution could not be found.
which normally I would interpret to mean that there is in fact no solution, but when I run
S = solve(eqns)
I do not get the same output, but:
(solvelib::cartesianPower(R_, 2) minus solvelib::VectorImageSet(matrix([[z], [0]]), z, C_)) intersect solvelib::VectorImageSet(matrix([[-(VA^4*x^5 - 2*L^5*VA*VB^3 - 2*L^5*VA^3*VB - L*VA^4*x^4 - 3*L^4*VA^4*x + L^4*VB^4*x + 4*L^5*VA^2*VB^2 - 5*L^2*VA^4*x^3 + 4*L^3*VA^4*x^2 + L^3*VB^4*x^2 + 4*L^2*VA^2*VB^2*x^3 - 5*L^3*VA^2*VB^2*x^2 + 4*L*VA^3*VB*x^4 + L^4*VA*VB^3*x + 9*L^4*VA^3*VB*x + L*VA^2*VB^2*x^4 + 2*L^2*VA*VB^3*x^3 + 2*L^3*VA*VB^3*x^2 - 8*L^3*VA^3*VB*x^2 - 8*L^4*VA^2*VB^2*x)/(L^4*VA^3 - L^4*VB^3 - L^4*VA*VB^2 + L^4*VA^2*VB)], [x]]), x, R_ intersect RootOf(z^6 + (z^5*(L*VA^4 + L*VA^2*VB^2 + 4*L*VA^3*VB))/VA^4 + (z^4*(- 7*L^2*VA^4 + 2*L^2*VA*VB^3 + 8*L^2*VA^3*VB + 6*L^2*VA^2*VB^2))/VA^4 + (z^3*(- 5*L^3*VA^4 + L^3*VB^4 + 6*L^3*VA*VB^3 - 6*L^3*VA^3*VB + 4*L^3*VA^2*VB^2))/VA^4 + (z^2*(7*L^4*VA^4 + 3*L^4*VB^4 + 6*L^4*VA*VB^3 - 2*L^4*VA^3*VB - 14*L^4*VA^2*VB^2))/VA^4 - (z*(6*L^5*VA^4 - 2*L^5*VB^4 - 16*L^5*VA^3*VB + 12*L^5*VA^2*VB^2))/VA^4 + (L^6*VA^4 + L^6*VB^4 - 4*L^6*VA*VB^3 - 4*L^6*VA^3*VB + 6*L^6*VA^2*VB^2)/VA^4, z)) intersect solvelib::VectorImageSet(matrix([[x], [y]]), [x, y], [Dom::Interval(0, Inf), Dom::Interval(0, [1])])
which does not have a solution for VA and VB.
Does anyone have an idea how I can get matlab's solve() to solve for [VA VB] in terms of r, L, and X?
Thank you!
Try using the ReturnConditions flag of the solve command. As in,
S = solve(eqns,[VA,VB],'ReturnConditions',true)
This will return a struct containing an array of solutions and required conditions for each.
I played around a bit with your example and hit the same roadblocks as you when making the same assumptions. If we start from zero assumptions, solve will find a set of solutions for the given equations. You could inspect the conditions for each of the given solutions and see what if any meet your original constraints?
The example given above yields results as long as VA and VB are not constrained to the Reals. Could there be a typo somewhere in your system of eqns?
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
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)
I have this equation in x and y:
(x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)) = 0.
Now I call the solver:
R_c = #(y)solve((x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)), x, 'Real', true);
which gives me the implicit solutions as a function of y. Now try
R_c(.3)
to find the explicit solution at y = 0.3. MATLAB's answer is:
ans =
0.42846617518653978966562924618638
0.15249587894102346284238111155954
0.12068186494007759990714181154349
However, the last entry in this array is NOT a solution. Test:
double(subs(subs((x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2)), x, .12068186494007759990714181154349), y, .3))
yields
-0.0585.
This is not a rounding error. The other 2 solutions work perfectly and solve the equation correctly. I wonder where MATLAB the third value gets from. Can anyone help?