Implicit Functions With 2 Variables - matlab

Thanks to the fimplicit function, I can plot implicit functions with 2 variables (x,y).
For a particular x, there is a particular y which makes F_imp=0. Now take this y as an input to another function g which produces z.
How can I plot x,z for x's between [0.1 1]?
Of course, I could have found the inverse of g(y) and replace in F(x,y) but there is not closed form of inverse of g(y).
Below are the functions I am dealing with:
F_imp = #(x,y) log(100-x*90) - x*log(10+0.9*y) - (1-x)*log(100-0.1*y);
fimplicit(F_imp,[0.1 1 0 100])
g=0.1*log(10+y*0.9)+0.9*log(100-0.1*y)

You can use the ImplicitFunctionLine object, which is an optional return value of the fimplicit() function. In this way you get access to the correspondent x and y data.
Then just use y to calculate g and plot g against x:
clear;
F_imp = #(x,y) log(100-x*90) - x*log(10+0.9*y) - (1-x)*log(100-0.1*y);
fp = fimplicit(F_imp,[0.1 1 0 100]); %returns the ImplicitFunctionLine object
%get calculated data points from the object
x = fp.XData;
y = fp.YData;
%set y as input for g
g=0.1*log(10+y*0.9)+0.9*log(100-0.1*y);
plot(x, g);
grid minor;
Here is the result:

Related

How can I plot this Octave\Matlab function output?

I everyone,
I am pretty new in Octave\MatLab and I have the following doubt. I have this code calculating a sigmoid function for a parameter z:
function g = sigmoid(z)
%SIGMOID Compute sigmoid function
% g = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g = zeros(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g = 1 ./ (1 + exp(-z));
% =============================================================
end
z could be a scalar, a vector or a matrix.
For example doing something like this:
>> X = [1 2; 0 5]
X =
1 2
0 5
>> g = 1 ./ (1 + exp(-X));
>> g
g =
0.73106 0.88080
0.50000 0.99331
Given the matrix X having 2 features X1 and X2 (the 2 column) how can I plot this function? The output is a three dimensional function?
The output is not a 3D function because you have one input X, and one output g, it's 2D.
You can display it column by column , displaying each column as a separate function on the same plot:
plot(X, g)
which is equivalent to:
figure
hold on
for i = 1:size(X,2)
plot(X(:,i), g(:,i))
end
hold off
You can display it row by row, displaying each row as a separate function on the same plot:
plot(X', g')
which is equivalent to:
figure
hold on
for i = 1:size(X,1)
plot(X(i,:), g(i,:))
end
hold off
You can display it as an array:
plot(X(:), g(:))

Plot square root in a contour

I have two variables x and y. I decide to plot the square root of their difference using contour as follows:
x=0:0.1:100;
y=0:0.1:100;
G=sqrt(x-y);
test2 = G;
test2(~(G<0)) = nan;
[C,h]=contourf(x,y,G,'ShowText','off');
set(gca,'FontSize',20)
However I get this error : Error using contourf (line 69)
Z must be size 2x2 or greater.
If that is resolved, I want to reach my goal and plot the actual function which relies on x, y and G itself as follows:
Function = 2 sqrt(x) / G * acoth((sqrt(x) + y/2 )/G )
I give an example
x=0:0.1:100;
y=0:0.1:100;
[X, Y]=meshgrid(x,y);
G=sqrt(X-Y);
test2 = G;
test2(~(G<0)) = nan;
[C,h]=contourf(X,Y,abs(G),'ShowText','off');
set(gca,'FontSize',20)
Input for contour should be 2D array, but your arrays are 1D.
Here, G is complex number. When you plot G, you should plot absolute G.
The result will be like below.
Regarding your function,
H=((2*sqrt(X))./G).*acoth((sqrt(X) + Y/2 )./G );

Using meshgrid in MATLAB when the function has matrix operations

I need to plot level surfaces of a 3 variable function. The variables are in a column vector X = [x, y, z]^t. The function is f(X) = X^t * A * X. Where ^t means transpose and A is a 3x3 constant matrix. I know for a fact that A is symmetric and therefore diagonalizable, i.e. A = V * D * V^t. Just in case it turns out to be useful.
I intend to use isosurface to get the points of the function where it equals a certain level and then use patch to plot.
However I can't figure out how to compute the value of the function for every point in the grid. Ideally I'd like to do
x = linspace(-1, 1,10); y=x; z = x;
[XX,YY,ZZ]=meshgrid(x,y,z);
f = [XX YY ZZ]'*A*[XX YY ZZ];
level = 1;
s = isosurface(XX,YY,ZZ,f,level);
patch(s, 'EdgeColor','none','FaceColor','blue');
but this won't work obviously because of the sizes of XX and A. What I've done so far is do the math myself to obtain the function as a polynomial of XX, YY and ZZ but it's incredibly ugly and not practical.
Anybody knows how to do this? Thanks!
If I understand correctly, this does that you want. In the following explanation I will use 10x10x10 points as given in your example (although the code works for any number of points). Also, I define a random 3x3 matrix A.
Once XX, YY and ZZ have been generated as 10x10x10 arrays (step 1), the key is to build a 1000x3 matrix in which the first column is x coordinate, the second is y and the third is z. This is variable XYZ in the code below (step 2).
Since XYZ is a matrix, not a vector, the function f can't be computed using matrix multiplication. But it can be obtained efficiently with bsxfun. First compute an intermediate 1000x3x3 variable (XYZ2) with all 3x3 products of coordinates for each of the 1000 points (step 3). Then reshape it into a 1000x9 matrix and multiply by the 9x1 vector obtained from linearizing A (step 4).
The f thus obtained has size 1000x1. You need to reshape it into a 10x10x10 array to match XX, YY and ZZ (step 5). Then you can use isosurface as per your code (step 6).
A = rand(3);
x = linspace(-1, 1,10); y = x; z = x;
[XX,YY,ZZ] = meshgrid(x,y,z); %// step 1
XYZ = [XX(:) YY(:) ZZ(:)]; %// step 2
XYZ2 = bsxfun(#times, XYZ, permute(XYZ, [1 3 2])); %// step 3
f = reshape(XYZ2,[],numel(A))*A(:); %// step 4
f = reshape(f, size(XX)); %// step 5
level = 1;
s = isosurface(XX,YY,ZZ,f,level);
patch(s, 'EdgeColor','none','FaceColor','blue'); %// step 6
I suspect that you could explicitly define your function as
ffun = #(x,y,z) [x y z]*A*[x; y; z];
then use arrayfun to apply this function to each element of your coordinate vectors:
f = arrayfun(ffun,XX,YY,ZZ);
This will return f(i)=ffun(XX(i),YY(i),ZZ(i)) for each i in 1:numel(XX), which I think is what you are after. The output f will have the same shape as your input arrays, which seems perfectly fine to use with isosurface later.

Matlab. Difference between plot and fplot?

Why use 'fplot' to plot functions when we can use 'plot'? I can't understand
With plot you have to manually define the x values and compute the corresponding y given by the function.
>> x = 0:.01:1;
>> y = sin(10*x);
>> plot(x,y,'.-')
With fplot you define the function generically, for example as an anonymous function; pass a handle to that function; and let Matlab choose the x values and compute the y values. As an example, take a difficult function:
>> f = #(x) sin(1/x);
Assume we want to plot that between 0.01 and 1:
>> lims = [.01 1];
>> fplot(f, lims, '.-')
See how Matlab does a pretty good job choosing closer x values in the left area, where the function becomes wilder.

How to plot a function of two variable one defined in Matlab

How do you plot a user-defined function of two variables in Matlab?
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
Z = my_func(X,Y);
surf(X,Y,Z);
Alternatively, if your function is not vectorized,
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
for x = 1:length(xs)
for y = 1:length(ys)
Z(x,y) = my_func(X(x,y), Y(x,y));
end
end
Z = my_func(X,Y);
surf(X,Y,Z);
ezsurf is a simple solution, or ezmesh, or ezcontour, or ezsurfc, or ezmeshc.
It has many types.
You can go plot gallery and select your variable and then types like mesh, 3D, surface, ...