I have this function:
psi = #(x,y)(-(11*x^(6/5)*y^(1/5) - 19*x + 9)/(10*x^(1/10)*y^(1/10)))
Solving for x can only be done implicitly:
R_sp = #(y)solve(psi(x,y), x);
I need to evaluate the following double integral with R_sp as the lower bound:
int(s * int(s*t, t, R_sp(s), 1), s, .3, .4)
When evaluating, matlab says: Error in MuPAD command: A list or a table of entries is expected. [(Dom::Matrix(Dom::ExpressionField()))::create]
The values of R_sp are well-defined in [.3, .4] and can be calculated easily:
R_sp(.3) = 0.845993307852830503940553192774
R_sp(.4) = 0.89641082924669996315283636152907
So I assume that the int function of matlab cannot process continuous values in that implicit function, right? Of course in this case, I can use the trapz function to approximate the integral above by calculating discrete value pairs of the integrand in a for-loop. But is there any other way in matlab to evaluate this integral other than using the trapz approximation (using continuous values)?
Related
mat_vec=zeros(100,10000);
a dot product is dot(xi, xj)
distanceFunction = #(xi, xj)dot(xi, xj)
mat_dist=pdist(mat_vec, distanceFunction)
Error Infomation like
distanceFunction =
function_handle with value:
#(xi,xj)dot(xi,xj)
Error using pdist
Error evaluating distance function '#(xi,xj)dot(xi,xj)'.
Error in Task1_lab404_02 (line 45)
mat_dist=pdist(mat_vec, distanceFunction)
Caused by:
Error using dot
A and B must be same size.
From pdist documentation (emphasis mine):
A distance function has the form
function D2 = distfun(ZI,ZJ)
where
ZI is a 1-by-n vector containing a single observation.
ZJ is an m2-by-n matrix containing multiple observations. The function must accept a matrix ZJ with an arbitrary number of observations.
You can achieve that if you write the function vectorized over the second input. For dot product this is very easy, using either matrix multiplication:
distanceFunction = #(xi, xj) xj*xi';
or implicit expansion:
distanceFunction = #(xi, xj) sum(conj(xi).*xj, 2);
Note, however, that this is not a true distance function, because for complex inputs distanceFunction(a, b) is not the same as distanceFunction(b, a). Applying pdist will only give one of those results for each pair.
The equation is 4*x^2-2*y^2==9. Using implicit differentiation, I can find that the second derivative of y with respect to x is -9/y^3, which requires a substitution in the final step.
I am trying to duplicate this answer using Matlab's symbolic toolbox. I did find some support for the first derivative here, and was successful finding the first derivative.
clear all
syms x y f
f=4*x^2-2*y^2-9
sol1=-diff(f,x)/diff(f,y)
But I am unable to continue onward to find the second derivative with the final simplification (replacing 4*x^2-2*y^2 with 9).
Can someone show me how to do this in Matlab?
To my knowledge there is no direct way to obtain an implicit second derivative in Matlab. And working with implicit functions in Matlab can be fairly tricky. As a start, your variable y is implicitly a function of x s you should define it as such:
clear all
syms y(x) % defines both x and y
f = 4*x^2-2*y^2-9
Here y(x) is now what is called an arbitrary or abstract symbolic function, i.e., one with no explicit formula. Then take the derivative of f with respect to x:
s1 = diff(f,x)
This returns a function in terms of the implicit derivative of y(x) with respect to x, diff(y(x), x) (in this case diff(y) is shorthand). You can solve this function for diff(y) algebraically with subs and solve:
syms dydx % arbitrary variable
s2 = subs(s1,diff(y),dydx)
s3 = solve(s2,dydx)
This yields the first implicit derivative. You can then take another derivative of this expression to obtain the second implicit derivative as a function of the first:
s4 = diff(s3,x)
Finally, substitute the expression for the first implicit derivative into this and simplify to obtain the final form:
s5 = simplify(subs(s4,diff(y),s3))
This yields (2*(y(x)^2 - 2*x^2))/y(x)^3. And then you can eliminate x using the original expression for f with further substitution and solving:
syms x2
f2 = subs(f,x^2,x2)
x2 = solve(f2,x2)
s6 = subs(s5,x^2,x2)
Finally, you can turn this back into an explicit algebraic expression with a final substitution, if desired:
s7 = subs(s6,y,'y')
This yields your solution of -9/y^3.
This whole process can be written more concisely (but very unclearly) as:
clear all
syms y(x) dydx x2
f = 4*x^2-2*y^2-9;
s1 = solve(subs(diff(f,x),diff(y),dydx),dydx)
s2 = simplify(subs(subs(subs(diff(s1,x),diff(y),s1),x^2,solve(subs(f,x^2,x2),x2)),y,'y'))
There are many other ways to achieve the same result. See also these two tutorials: [1], [2].
It's been almost four years since I asked this question, and got brilliant help from horchler, but I've discovered another method using the chain rule.
syms x y
f=4*x^2-2*y^2-9
dydx=-diff(f,x)/diff(f,y)
d2ydx2=diff(dydx,x)+diff(dydx,y)*dydx
d2ydx2=simplifyFraction(d2ydx2,'Expand',true)
s1=solve(f,x)
subs(d2ydx2,x,s1(2))
I am in trouble to implement the following double integral. there is a summation inside the integral which make things a bit complicated. The matlab code I did is as follows and always has error like "Matrix dimensions must agree." , any hint to implement it? thanks
n=3;
nn=1:n;
aa=gamma([1:n])
thre=3;
lapha=4;
r=3;
fun1= #(theta, x) (1-sum( lambda *pi *( (x-r).^2+r^2-(x-r).*r.*cos(theta)).^(nn-1)./aa).*exp(-1*lambda *pi*((x-r).^2+r^2-(x-r).*r.*cos(theta)))).*lambda/n*(1-1/2^n).*thre.*r.^alpha.*(x-r).^(1-alpha) ;
answer=integral2( fun1, 0, 2*pi, 0, inf )
The double integral:
Your problem is the way you calculate the sum in the integrated function. The documentation of intergal2 says that the function argument must accept arrays X and Y of the same size and return an array of corresponding values. But this expression inside the function definition:
((x-r).^2 + r^2 - (x-r).*r.*cos(theta)).^(nn-1)./aa
is not working the way you expect, because it's you who decide the size of nn and aa, while it's integral2 that decides the size of the vectors x and theta; no wonder that there are disagreements on it.
I want to evaluate the simple example of integral
a = max(solve(x^3 - 2*x^2 + x ==0 , x));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
and the error is
Error using integral (line 85)
A and B must be floating-point scalars.
Any tips? The lower limit of my integral must be a function, not a number.
The Matlab command solve returns symbolic result. integral accepts only numeric input. Use double to convert symbolic to numeric. As your code is written now, already max should throw an error due to symbolic input. The following works.
syms x;
a = max(double(solve(x^3 - 2*x^2 + x)));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
Output: 1.9331.
the lower limit of my integral must be a function, not a number
integral is a numeric integration routine; the limits of integration must be numeric.
Check values of a by mouse over in breakpoint or removing the ; from the end of the line so it prints a. Based on the error, a is not a scalar float. You might need another max() or double() statement to transform the vector to a single value.
Solve Help : http://www.mathworks.com/help/symbolic/solve.html
Integral Help : http://www.mathworks.com/help/ref/integral.html
I have the following ODE:
x_dot = 3*x.^0.5-2*x.^1.5 % (Equation 1)
I am using ode45 to solve it. My solution is given as a vector of dim(k x 1) (usually k = 41, which is given by the tspan).
On the other hand, I have made a model that approximates the model from (1), but in order to compare how accurate this second model is, I want to solve it (solve the second ODE) by means of ode45. My problem is that this second ode is given discrete:
x_dot = f(x) % (Equation 2)
f is discrete and not a continuous function like in (1). The values I have for f are:
0.5644
0.6473
0.7258
0.7999
0.8697
0.9353
0.9967
1.0540
1.1072
1.1564
1.2016
1.2429
1.2803
1.3138
1.3435
1.3695
1.3917
1.4102
1.4250
1.4362
1.4438
1.4477
1.4482
1.4450
1.4384
1.4283
1.4147
1.3977
1.3773
1.3535
1.3263
1.2957
1.2618
1.2246
1.1841
1.1403
1.0932
1.0429
0.9893
0.9325
0.8725
What I want now is to solve this second ode using ode45. Hopefully I will get a solution very similar that the one from (1). How can I solve a discrete ode applying ode45? Is it possible to use ode45? Otherwise I can use Runge-Kutta but I want to be fair comparing the two methods, which means that I have to solve them by the same way.
You can use interp1 to create an interpolated lookup table function:
fx = [0.5644 0.6473 0.7258 0.7999 0.8697 0.9353 0.9967 1.0540 1.1072 1.1564 ...
1.2016 1.2429 1.2803 1.3138 1.3435 1.3695 1.3917 1.4102 1.4250 1.4362 ...
1.4438 1.4477 1.4482 1.4450 1.4384 1.4283 1.4147 1.3977 1.3773 1.3535 ...
1.3263 1.2957 1.2618 1.2246 1.1841 1.1403 1.0932 1.0429 0.9893 0.9325 0.8725];
x = 0:0.25:10
f = #(xq)interp1(x,fx,xq);
Then you should be able to use ode45 as normal:
tspan = [0 1];
x0 = 2;
xout = ode45(#(t,x)f(x),tspan,x0);
Note that you did not specify what values of of x your function (fx here) is evaluated over so I chose zero to ten. You'll also not want to use the copy-and-pasted values from the command window of course because they only have four decimal places of accuracy. Also, note that because ode45 required the inputs t and then x, I created a separate anonymous function using f, but f can created with an unused t input if desired.