How to plot 4D graph either in python or matlab? - matlab

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?

Related

Expected Value of given two parameters

I would like to know how to compute:
E[Y | X_i, X_j]
on MATLAB.
Let's say
x = unifrnd(0,1,1000,2) and the model is y = x1 + x2.
suppose that you have scatter plot x1 vs Y and x2 vs Y. With these scatter plots, you compute the regression coefficient (i.e. f1 = ax1 + b and f2 = cx2 + d).
now I want to compute expected value of Y given x1 and x2.
My question is how do I do that ? Thanks in advance

Solving system of three equations with three unknown variables

I am trying to solve a system of three equations with three unknown variables.
A1=(x+y)/2+(x-y)/2*cos(2*phi)+z*sin(2*phi)/2
A2=(x+y)/2-(x-y)/2*cos(2*phi)-z*sin(2*phi)/2
A3=-(x-y)/2*sin(2*phi)+z*cos(2*phi)
where A1, A2, A3, and phi are known and x,y, and z are unknown.
I used below code but it does not work. I got the solution as symbols.
clear;
clc;
A1=50;
A2=37.5;
A3=125.6;
phi=28;
syms x y z
eqn1 = (x+y)/2+(((x-y)/2)*cosd(2*phi))+(z*sind(2*phi))/2== A1;
eqn2 = (x+y)/2+(((x-y)/2)*cosd(2*phi))-(z*sind(2*phi))/2== A2;
eqn3 = (((x-y))*sind(2*phi))+(z*cosd(2*phi))== A3;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z]);
X_1 = linsolve(A,B);
Thanks
You can skip using equationsToMatrix and linsolve and just use solve. You are already using the symbolics toolbox, so why would you want to convert the system into a matrix of coefficients and solve it that way? Just use the actual equations directly.
A1=50;
A2=37.5;
A3=125.6;
phi=28;
syms x y z
eqn1 = (x+y)/2+(((x-y)/2)*cosd(2*phi))+(z*sind(2*phi))/2== A1;
eqn2 = (x+y)/2+(((x-y)/2)*cosd(2*phi))-(z*sind(2*phi))/2== A2;
eqn3 = (((x-y))*sind(2*phi))+(z*cosd(2*phi))== A3;
[X, Y, Z] = solve(eqn1, eqn2, eqn3);
I get:
X = (sym)
69370560820559
──────────────
926177760500
Y = (sym)
-61526962823521
────────────────
926177760500
Z = (sym)
2910
────
193
Note that I'm using Octave instead of MATLAB (on my current system, I don't have access to the symbolic toolbox) so the output may be a bit different in format. You probably also want this in real (floating-point) form, so an additional cast to double for the outputs should help:
X = double(X);
Y = double(Y);
Z = double(Z);
By doing this, we get:
>> format long g;
>> X
X = 74.8998343288972
>> Y
Y = -66.4310518429048
>> Z
Z = 15.0777202072539

Plot 3D plane given equations in matlab

I want to plot 2 3D planes for the equations given below :
x + y + z = 1
2x - y = 0
For 1st equation, I plotted it using meshgrid as :
[x y] = meshgrid(-5:0.5:5);
z = 1 - x - y
mesh(x,y,z)
But for 2nd equation, z is not given i.e. z can be anything, then how do I plot plane for this ?
The comments are correct. It is more of a math problem. You draw a line 2x - y = 0 and translate it for any z value to create a plane.
[x, y] = meshgrid(-5:0.5:5);
Zv = #(x,y) 1 - x - y;
mesh(x,y,Zv(x,y));
hold on
[x, z] = meshgrid(-5:0.5:5);
Yv = #(x) 2*x;
mesh(x,Yv(x),z);
hold off

MATLAB: Apply input vector to a vector of functions

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})

Plotting a 3D graph of `sqrt(1+1/(kr)^2)`

I am trying to plot the following equation in MATLAB:
ratio = sqrt(1+1/(kr)^2)
With k and r on the x and y axes, and ratio on the z axis. I used meshgrid to create a matrix with values for x and y varying from 1 to 10:
[x,y] = meshgrid([1:1:10],[1:1:10]);
The problem now is to create values for z. I've tried to just type the whole equation in, but that gives this result:
>> Z = sqrt(1+1/(x .* y)^2)???
Error using ==> mldivide
Matrix dimensions must agree.
So what I did is go to through the whole process manually, which produces the right graph in the end:
z = z^2;
z = 1 ./ z;
z = 1 + z;
z = sqrt(z);
mesh(x,y,z)
Is there a more elegant way to do this? Or a way to type in the equation and let MATLAB handle the rest?
Try this:
Z = sqrt(1+1./(x .* y).^2);
surf(Z);
The problem that you had is related to using / instead of ./ and ^2 instead of .^2