The question image
I have write the MATLAB code, like this
syms x x1 x2 x3 y y1 y2 y3 t0 t1 t2 t3 v
[x,y]=solve((x-x1)^2+(y-y1)^2-(v*(t1-t0))^2==0,(x-x2)^2+(y-y2)^2-(v*(t2-t0))^2==0,(x-x3)^2+(y-y3)^2-(v*(t3-t0))^2==0)
but the ans is empty, like this
x =
Empty sym: 0-by-1
y =
Empty sym: 0-by-1
In fact, the equation set does have a solution, so I want to know how to correct the code?
The docs say
If solve returns an empty object, then no solutions exist.
Cleaning up your problem a bit,
Eq1 = (x-x1)^2+(y-y1)^2-(v*(t1-t0))^2==0;
Eq2 = (x-x2)^2+(y-y2)^2-(v*(t2-t0))^2==0;
Eq3 = (x-x3)^2+(y-y3)^2-(v*(t3-t0))^2==0;
EqSys = [Eq1,Eq2,Eq3]; % create a system of equations
solve(EqSys,x) % <= specify, for which variable the system of equations should be solved
made me feel that you want to solve vectors rather than distinct variables, right? Are the entries independent? Or is the solution x indeed a scalar value?
If not, solving a single equation
solve(Eq1,x)
does return an answer
ans =
x1 + (y - y1 + t0*v - t1*v)^(1/2)*(y1 - y + t0*v - t1*v)^(1/2)
x1 - (y - y1 + t0*v - t1*v)^(1/2)*(y1 - y + t0*v - t1*v)^(1/2)
Related
I have a differential equation that looks like this:
dT/dx = (1+alpha\*M1)\*(T^2)\*S(x) - 4\*(2+gamma\*M2)\*x\*T
I want to differentiate this equation let's say over alpha. However, T depends on alpha. How can I get a differential equation that includes the derivative dT/da so I can solve for it (using ode45 or something)?
Note: I can't differentiate the equation by hand as I will need to do this for the Ys as well, where I don't have an easy analytical function.
My code so far:
syms L alpha gamma Y2 Y3 Y4 Y5 Y6 x T X
M1 = 0;
M2 = 7;
Y1 = 1 + M2 / 10;
Y7 = 3 + M1 / 10;
xcp = [0 0.1*L 0.25*L 0.5*L 0.6*L 0.75*L L];
ycp = [Y1 Y2 Y3 Y4 Y5 Y6 Y7];
T0 = 5 + 1/L - 25/(L^2);
S = bezier_syms(); % Assume this returns a function of L,x,Y2,Y3,Y4,Y5,Y6
ode = diff(T,x) == (1+alphaM1)*(T^2)*S(L,x,Y2,Y3,Y4,Y5,Y6) - 4*(2 + gamma*M2)xT;
Given the equation
dT/dx = F(x,T,a)
and U=dT/da, then by the rules of differentiation like the chain rule, you get
dU/dx = dF/da(x,T,a) + dF/dT(x,T,a)*U
where the partial derivatives are easy to determine. For more precise formulas, see https://math.stackexchange.com/a/3699281/115115
I have an equation like this:
dy/dx = a(x)*y + b
where a(x) is a non-constant (a=1/x) and b is a vector (10000 rows).
How can I solve this equation?
Let me assume you would like to write a generic numerical solver for dy/dx = a(x)*y + b. Then you can pass the function a(x) as an argument to the right-hand side function of one of the ODE solvers. e.g.
a = #(x) 1/x;
xdomain = [1 10];
b = rand(10000,1);
y0 = ones(10000,1);
[x,y] = ode45(#(x,y,a,b)a(x)*y + b,xdomain,y0,[],a,b);
plot(x,y)
Here, I've specified the domain of x as xdomain, and the value of y at the bottom limit of x as y0.
From my comments, you can solve this without MATLAB. Assuming non-zero x, you can use an integrating factor to get a 10000-by-1 solution y(x)
y_i(x) = b_i*x*ln(x) + c_i*x
with 10000-by-1 vector of constants c, where y_i(x), b_i and c_i are the i-th entries of y(x), b and c respectively. The constant vector c can be determined at some point x0 as
c_i = y_i(x0)/x_0 - b_i*ln(x0)
Here is the equation in WolframAlpha returning me the correct answer.
In MATLAB, I've written the following:
mu = 305; %as a temporary example since it's greater than c, which is 300
syms x
eqn = ((1 + (x/(mu + 300)))^((1/2) + (150/mu)))*((1 - (x/(mu - 300)))^((1/2) - (150/mu))) - 0.2 == 0 %matlab shows the answer is -605
solve(eqn,x)
It's the same equation, except MATLAB substitutes for mu for me. MATLAB is returning the following:
eqn = logical 0
ans = x
Am I typing the equation in wrong somehow? Is that why it's showing me a logical zero when I'm not suppressing the equation? How do I get it to result in the same values as WolframAlpha?
I also want to note that Maple seems to hang on this same equation as well.
Unless you have a specific reason for using symbolic expressions, you can solve the equation that you have using fsolve as follows:
%Define your value of mu
mu = 305;
% Define the equation as an anonymous function
fn = #(x) ((1 + (x/(mu + 300)))^((1/2) + (150/mu)))*((1 - (x/(mu - 300)))^((1/2) - (150/mu))) - 0.2;
% Define the initial value for x so that fsolve can find the root nearest to that
x0 = 1;
root_x = fsolve(fn, x0);
This leads to the output root_x = 5.0000 + 0.0000i
You can also change the initial value of x0
x0 = 400;
root_x = fsolve(fn, x0);
This will return the output root_x = -4.9005e+02 - 2.6326e-05i
This method can be used to solve any of the equations that you might have.
I have an equation:
dC(t)/dt = -K*C + G
I used MATLAB to solve this equation and I got this solution:
C(t) = G/K + (Co-G/K)exp(-Kt/V)
How can I rearrange this equation to get K=?
Because your equation has K inside and outside the exponential, you can't get a nice closed form solution, so the best you hope to achieve is a numerical approximation.
>> syms t C(t) K G C0
>> D=dsolve(diff(C)==-K*C+G,C(0)==C0) %// solve the ODE with an initial condition
D =
(G - exp(-K*t)*(G - C0*K))/K
%// Solve for k given particular values of the other variables
>> k=solve(subs(C(t)==D,{G,t,C,C0},{1,2,1,0.5}),K)
k =
0.91228212986814722960889983912519
If you ignore the initial condition, you can get an equation, but it is in terms of the Lambert W function, and is really not useful for anything.
>> syms t C(t) K G
>> D=dsolve(diff(C)==-K*C+G)
D =
(G - C2*exp(-K*t))/K
>> solve(C==D,K)
ans =
(G + (C(t)*lambertw(0, -(C2*t*exp(-(G*t)/C(t)))/C(t)))/t)/C(t)
I am trying to use the Matlab "gradient" and "hessian" functions to calculate the derivative of a symbolic vector function with respect to a vector. Below is an example using the sigmoid function 1/(1+e^(-a)) where a is a feature vector multiplied by weights. The versions below all return an error. I am new to Matlab and would very much appreciate any advice. The solution may well be under my nose in the documentation, but I have not been able to solve the issue. Thank you in advance for your help!
%version 1
syms y w x
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
gradient(f, w)
%version 2
syms y w1 w2 w3 x1 x2 x3 x w
x = [x1,x2,x3];
w = [w1,w2,w3];
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
%version 3
syms y w1 w2 w3 x1 x2 x3
f = (y-1)*[w1,w2,w3].*[x1,x2,x3] + log(1/(1+exp(-[w1,w2,w3].*[x1,x2,x3])));
Thanks, Daniel - it turns out the problem was in not having used dot() to take the dot product of w and x. Both diff() and gradient() gave the same solution, shown below:
syms y
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*dot(w,x) + log(1/(1+exp(dot(-w,x))));
diff(f, w(1))
gradient(f, w(1))
%ans =
%x1*(y - 1) + (x1*exp(- x1*conj(w1) - x2*conj(w2) - x3*conj(w3)))/
(exp(-x1*conj(w1) - x2*conj(w2) - x3*conj(w3)) + 1)