Say I have the following definitions:
syms x y z
F = [x^2+y^2+z^2-1; 2*x^2+y^2-4*z; 3*x^2-4*y+z^2];
v = [x, y, z];
I want to evaluate F(x=1, y=2, z=3), meaning apply the stored functions to the input.
Alternatively, I could use function handlers for every member of F, but then I couldn't easily calculate the jacobian:
J = jacobian(F, v)
How can it be done?
To evaluate F for some specific values you can use
subs(F,{x y z},{1 2 3})
Related
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.
I am trying to make a 3d plot but i am getting an error and im not sure how to solve it. I know that there are other questions out there similar to mine but i tried some of them and it did not work.
fh = sin(x)*cos(y).^3 + 2*cos(x).^5*sin(y)
[X,Y] = meshgrid(1:0.5:10,1:20);
surf(X,Y,fh)
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.
The Z data in this case is what you are passing to surf as fh. It looks like fh is the function you want to use to compute Z, but you need to use the gridded values you generated for X and Y to evaluate it. As your code is now, it is evaluating the function using x and y (case matters!), which you haven't defined for us. Try this instead:
[X, Y] = meshgrid(1:0.5:10, 1:20);
Z = sin(X).*cos(Y).^3 + 2.*cos(X).^5.*sin(Y);
surf(X, Y, Z);
Notice that I used the .* operator (element-wise multiplication) instead of the * operator (matrix multiplication) in the equation.
You could also do this by defining an anonymous function that evaluates the formula for a given set of data:
fh = #(x, y) sin(x).*cos(y).^3 + 2.*cos(x).^5.*sin(y);
[X, Y] = meshgrid(1:0.5:10, 1:20);
surf(X, Y, fh(X, Y));
Let's say you have some vector z and you compute [f, x] = ecdf(z);, hence your empirical CDF can be plotted with stairs(x, f).
Is there a simple way to compute what all the percentile scores are for z?
I could do something like:
Loop through z, that is for each entry z(i) of z
Binary search through sorted vector x to find where z(i) is. (find index j such that x(j) = z(i))
Find the corresponding value f(j)
It feels like there should be a simpler, already implemented way to do this...
Let f be a monotone function defined at values x, for which you want to compute the inverse function at values p. In your case f is monotone because it is a CDF; and the values p define the desired quantiles. Then you can simply use interp1 to interpolate x, considered as a function of f, at values p:
z = randn(1,1e5); % example data: normalized Gaussian distribution
[f, x] = ecdf(z); % compute empirical CDF
p = [0.5 0.9 0.95]; % desired values for quantiles
result = interp1(f, x, p);
In an example run of the above code, this produces
result =
0.001706069265714 1.285514249607186 1.647546848952448
For the specific case of computing quantiles p from data z, you can directly use quantile and thus avoid computing the empirical CDF:
result = quantile(z, p)
The results may be slightly different depending on how the empirical CDF has been computed in the first method:
>> quantile(z, p)
ans =
0.001706803588857 1.285515826972878 1.647582486507752
For comparison, the theoretical values for the above example (Gaussian distribution) are
>> norminv(p)
ans =
0 1.281551565544601 1.644853626951472
I want to use a vector input such of dimension [m;1] in a function that takes in m number of inputs. For example:
syms x1 x2;
f = x1^2 + x2^2;
F = matlabFunction(f);
x = [1;1];
F(x);
The above code does not work because F is a function of 2 inputs and it only sees the vector x as a single input. I know I can say F(x(1),x(2)) and the above would work but I want it to work for a function of m variables and a vector of m length.
All help is appreciated.
Thanks.
You will want to first convert x to a cell and then pass it with {:}.
xcell = num2cell(x);
F(xcell{:});
Alternately, you can specify that you want x1 and x2 to be passed as an array when you call matlabFunction using the Vars parameter.
F = matlabFunction(f, 'Vars', {[x1, x2]});
F([1 1]);
I have two functions f and g. Both are dependent on three variables being x, y and z. The functions and boundaries of x, y and z are provided as follows:
x = 150:5:250;
y = 0.2:0.05:0.3;
z = 1:0.1:2;
f = 20./(x.^0.2) .* (y.^0.3) .* (z.^0.01);
g = x .* y .* z
How can both of the functions be plotted in one graph?