Double integral - Matlab - matlab

Trying to find double integral of that function:
Matlab code:
x = -1:0.05:1;
y = 0:0.05:1;
[x,y] = meshgrid(x);
F = sqrt((x).^2.*y) * ((sin((x).^2 .* (y).^2)).^3) - ((cos((x).^3.*(y).^3)).^5);
surfl(x,y,F);
colormap summer;
shading interp;
dblquad('sqrt((x).^2.*y) * (sin((x).^2 .* (y).^2)).^3 - (cos((x).^3.*(y).^3)).^5', -1, 1, 0, 1)
Errors:
Untitled
Error using surf (line 57)
X, Y, Z, and C cannot be complex.
Error in surfl (line 129)
h = surf(cax,x,y,z);
Error in Untitled (line 5)
surfl(x,y,F);
How can I solve these errors?

In this line:
[x,y] = meshgrid(x);
You are basically doing:
[x,y] = meshgrid(x, x);
So basically x and y both go from -1 to 1 and since the equation has a sqrt(x.^2 .* y), you will get complex numbers. To generate a mesh using x, y with the bounds you specified for x,y use:
[x,y] = meshgrid(x, y);

Related

Error in mesh(X,Y,Z) in matlab

This is my code:
load mwe16_2.dat;
x = mwe16_2(:,1); % x contains the 1st column(500 values) of ‘mwe16_2’
y = mwe16_2(:,2); % y contains the 2nd column (500 values)of ‘mwe16_2’
z = mwe16_2(:,3); % z contains the 3rd column(500 values) of ‘mwe16_2’
[X, Y, Z] = meshgrid (x, y, z);
mesh (X, Y, Z)
While running the code it is showing the error:
*Error in plot3_d (line 13) mesh(X,Y,Z) in Matlab*
Can someone say the reason for the error and how to correct it?
[X,Y,Z] = meshgrid(x,y,z) produces three 3D arrays from x, y and z vectors. Now if you take a look at mesh you will see that you are not providing this function with proper input.
If you want to illustrate the set of points defined by three vectors of x, y and z, you can consider using scatter3:
figure; scatter3(x, y, z)

How can I plot a surface generated by a function f(x,y) where the values x and y are subject to constraints?

I'm new to Matlab and I have a function
and
How can I plot:
Define your X and Y in a linear array
X = linspace(-2, 2, 1000);
Y = linspace(-2, 2, 1000);
Mesh them so you have a grid of x and y
[x,y] = meshgrid(X,Y);
Get the value for your function
f = sqrt(x.^2 + y.^2);
Define your domain
D = (1 <= x.^2 + y.^2);
Set everything outside your domain to nan's so it won't plot
f(~D) = nan;
Plot the surface
surf(x,y,f, 'linestyle', 'none')

Plotting a 3d Function using Meshgrid and Surfc

I know this seems like a basic question—and it probably is. I am having trouble learning how to plot this function using meshgrid and surfc. I keep getting the error saying that "The surface Z must contain more than one row or column." Why am I getting this error? Here's what I have to do:
x = 0:.01:1;
y = 0:.01:1;
f = #(x, y) sin(4*pi.*x).*sin(y).*(1-y) + (1./sqrt(1+10.*(x.^2)+ 100.*(y-0.5).^2))
Z = f(x, y);
[X, Y] = meshgrid(x, y)
surfc(X, Y, Z)
Got it:
x = 0:.01:1;
y = 0:.01:1;
f = #(x, y) sin(4*pi.*x).*sin(y).*(1-y) + (1./sqrt(1+10.*(x.^2)+ 100.*(y-0.5).^2))
[X, Y] = meshgrid(x, y)
Z = f(X, Y);
size(x)
size(Y)
size(Z)
surfc(X, Y, Z)

Contour plot for 3D vector

I am trying to produce a contour plot for the 3D vectors returned by a custom function in the xy plane where z = 0.
I tried this but I just get an empty graph:
% Stand in for the real function I want to plot.
f = #(x, y, z) [x ^ 2, y ^ 2, x * y + z];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y), 3);
% I know this can be vectorized but the function I really want to graph can't
% be.
for i = 1:length(x)
for j = 1:length(y)
z(i, j, :) = f(x(i), y(j), 0);
end
end
figure;
axis equal;
contour(x, y, z);
You should mention what your axis will be. You have x,y and 3 outputs from f.
If you consider 3 outputs of your f as the ones to be plotted then you should use,
contour(z(:,:,1),z(:,:,2),z(:,:,3));
Which will give you this,
I think what you are looking for, is a function with one output, like,
f = #(x, y,z) [x ^ 2 + y ^ 2 + x * y + z ];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y));
for i = 1:length(x)
for j = 1:length(y)
z(i, j) = f(x(i), y(j),0);
end
end
contour(x,y,z,20);

mesh gaussian after rotation matrix in matlab

I am trying to rotate the derivative of a gaussian (or the original guassian for that matter) by applying a rotation matrix to the X,Y coordinates and then plotting it as a mesh in matlab, but I'm running into an issue that the plot will only rotate by 90 degress each time and for all n*pi points there is no mesh appearing at all. I am wondering what I am doing wrong, hopefully someone can spot my error. I'm fairly new to matlab so forgive me if the code is not pretty. Thank you!
This is the code that I have:
sigma = 4;
[x,y] = deal(-3*sigma:.5:3*sigma);
[X,Y] = meshgrid(x,y);
B = [transpose(x) transpose(y)];
for i=1:1:16
figure(i);
theta = i*pi/8;
rotation = [cos(theta) sin(theta); -sin(theta) cos(theta)];
A = B * rotation;
[x_new, y_new] = meshgrid(A(:,1)', A(:,2)');
mesh(x_new, y_new, dgauss_x(x_new, y_new, sigma));
end
function f = dgauss_x(x, y, sigma)
%first order derivative of Gaussian
f = -x .* gaussian(x, y, sigma) ./ sigma^2;
function f = gaussian(x, y, sigma)
f = exp(-(x .^ 2 + y .^ 2)/(2*sigma^2)) / (sqrt(2*pi*(sigma^2)));
I figure it out. It was a dumb error. Basically I had to create a meshgrid for X and Y, reshape it into an Nx2 matrix of X and Y. Apply the rotation matrix, then reshape back and apply the Gaussian.
The code looks as follows (could be cleaned up):
for i=1:1:16
figure(i);
theta = i*pi/8;
rotation = [cos(theta) -sin(theta); sin(theta) cos(theta)];
X1 = reshape(X, length(x)*length(x), 1);
Y1 = reshape(Y, length(y)*length(y), 1);
B2 = [X1 Y1];
A = B2 * rotation;
X1 = A(:,1);
X1 = reshape(X1, length(x), length(x));
Y1 = A(:,2);
Y1 = reshape(Y1, length(y), length(y));
mesh(X1, Y1, dgauss_x(X1, Y1, sigma));
end