solve integral symbolic in Matlab - matlab

I would like to symbolically solve an integral in Matlab. But Matlab doesn't solve my equation.
The term I want to take integral is : x ^2 * exp(-lambda*x)/ (x+w+tau)
Here is my code:
syms x
f = x ^2 * exp(-lambda*x)/ (x+w+tau);
F = int(f,0,lambda)
pretty(F)
Matlab does not solve it, instead gives me the folowing:
F =
int((x^2*exp(-lambda*x))/(tau + w + x), x, 0, lambda)

Related

How do I make this continuous regression?

Given the function f(x) = ln(x) with x belonging to [1,e] You must develop an algorithm in matlab that performs the following tasks.
Perform the continuous regression of f(x) onto the subspace 〈1, x, x^2〉, using the symbolic tools in Matlab. Plot the result (the function and the fitting).
Perform the discrete regression of f onto 〈1, x, x^2〉. For that purpose:
2.1. Introduce as regular sampler of the interval [1, e] using 1000 points. Call this sampler xs.
2.2. Evaluate f(x) on xs, and the basis functions 1, x, x^2 to generate {1, x, x^2, f}.
2.3. Perform the regression of f onto 〈1, x, x^2〉.
2.4. Plot the result (the function and the fitting)
i tried with this but it is not giving the right answer, and it is giving an error in plot(xs, f_values)
clear all
close all
clc
syms x
f = log(x);
V = [1 x x^2];
coeffs = V\f;
a0 = coeffs(1);
a1 = coeffs(2);
a2 = coeffs(3);
fitting = a0*1+ a1*x + a2*x^2;
ezplot(f,[1,exp(1)])
hold on
ezplot(fitting,[1,exp(1)])
legend('f(x)','fitting')
xs = linspace(1,exp(1),1000);
linspace(1,exp(1),1000)
f_values = subs(f, x, xs);
basis_1 = ones(1, 1000);
basis_x = xs;
basis_x2 = xs.^2;
V = [basis_1; basis_x; basis_x2];
fitting_values = coeffs(1)*basis_1 + coeffs(2)*basis_x + coeffs(3)*basis_x2;
plot(xs, f_values)
hold on
plot(xs, fitting_values)
legend('f(x)', 'fitting')
Looking solely at your error in plot(xs, f_values):
MATLAB's page for the function plot shows that when accepting two inputs (in your case, xs and f_values), they are either (x, y), or (y, line options). But since one of the values you've inputted are based on x (a symbolic value), it's a symbolic vector. plot can't handle symbolic inputs.
I believe the equivalent for syms is fplot from the symbolic maths toolbox.

Y from X dependency in Matlab

I have solved a system of differential equations and got the answer:
x(t) = -C1exp(-t) + C2exp(3*t)/3
y(t) = C1exp(-t) + C2exp(3*t)
X and y depend on t. How can I plot the y(x) graph?

How to numerically solve a system with two matrices in Matlab?

I'm trying to numerically find the solution to A*cos x +B*sin x = C where A and B are two known square matrices of the same size (for example 100x100), and C is a known vector (100x1).
Without the second term (i.e. with a single matrix), I will use Jacobi or Gauss-Seidel to solve this problem and get x but here, I don't see how to proceed to solve this problem in Matlab.
May be, it would be useful to solve the problem as : A*X + B*sqrt(1-X^2) = C.
I would greatly appreciate any help, ideas or advices
Thanks in advance
If I understood you correctly, you could use fsolve like this (c and X are vectors):
A = ones(2,2);
B = ones(2,2);
c = ones(2,1);
% initial point
x0 = ones(length(A), 1);
% call to fsolve
sol = fsolve(#(x) A * cos(x) + B*sin(x) - c, x0);
Here, we solve the nonlinear equation system F(x) = 0 with F: R^N -> R^N and F(x) = A * cos(x) + B*sin(x) - c.
Only for the sake of completeness, here's my previous answer, i.e. how one could do it in case C and X are matrices instead of vectors:
A = ones(2,2);
B = ones(2,2);
C = ones(2,2);
% initial point
x0 = ones(numel(A), 1);
% call to fsolve
fsolve(#(x) fun(x, A, B, C), x0)
function [y] = fun(x, A, B, C)
% Transform the input vector x into a matrix
X = reshape(x, size(A));
% Evaluate the matrix equation
Y = A * cos(X) + B*sin(X) - C;
% flatten the matrix Y to a row vector y
y = reshape(Y, [], 1);
end
Here, the idea is to transform the matrix equation system F: R^(N x N) -> R^(N x N) into a equivalent nonlinear system F: R^(N*N) -> R^(N*N).

How to solve a differential equation with non-constant coefficient?

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)

Rearranging symbolic expression

I have a function in MuPAD in terms of various variables as z = f(x,y,...).
Now I would like to rearrange the equation to express x in terms of z. I have not found the suitable command to do so.
Use solve. Example:
syms x y z
X = solve('z = x^3 + y^3 - 1', x);