I need to create matrices Dx and Dy that give the partial diferentials in the x and y directions respectivly when multipled by column stack representation of 2D area (x) so that Dx*x will give me the partial derivetives in the x direction in vector form as well (the same dimentions as x).
The derivitives are aproximated by:
f'(x,y) = 0.5*(f(x+1,y)-f(x-1,y)) for all x > 1 and x < n (the number of rows)
f'(1,y) = f(2,y)-f(1,y)
f'(n,y) = f(n,y)-f(n-1,y)
for Dx and similarly for Dy.
I tried creating the matrices using the fallowing code:
[X,Y] = meshgrid(1:n,1:m); % m,n - the dimentions of x
X = X(:)';
Y = Y(:)';
Dx = sparse(m*n,m*n);
Dy = sparse(m*n,m*n);
for x = 1:n
for y = 1:m
idx = y + (x-1)*m;
if (x == 1)
Dx(idx,:) = ((X == x+1) .* (Y == y)) - ((X == x) .* (Y == y));
elseif (x == n)
Dx(idx,:) = ((X == x) .* (Y == y)) - ((X == x-1) .* (Y == y));
else
Dx(idx,:) = 0.5*(((X == x+1) .* (Y == y)) - ((X == x-1) .* (Y == y)));
end
if (y == 1)
Dy(idx,:) = ((X == x) .* (Y == y+1)) - ((X == x) .* (Y == y));
elseif (y == m)
Dy(idx,:) = ((X == x) .* (Y == y)) - ((X == x) .* (Y == y-1));
else
Dy(idx,:) = 0.5*(((X == x) .* (Y == y+1)) - ((X == x) .* (Y == y-1)));
end
end
end
But this takes too much time, I feel that there should be a faster to create Dy and Dx?
About x: for a 2D function represented by X so that size(X) is [n m], then x=X(:) and size(x) is [n*m 1]
Follow those steps:
Represent the operation using a Filter. Derivative Filters are easy the way you defined the above.
Create the Convolution Matrix using MATLAB's convmtx2() Function.
This will get you what you wanted.
Derivative Filters in Image Processing
Horizontal Derivative:
* Forward Finite Difference - $ [-1, 1] $.
* Backward Finite Difference - $ [1, -1] $.
Vertical Derivative:
* Forward Finite Difference - $ [-1; 1] $.
* Backward Finite Difference - $ [1; -1] $.
Related
I'm looking for an easy, elegant way to create a triangle mesh of a torus with an arbitrary number of holes. Here are some examples.
First, the AI had a problem with the question title, then a human decided "Closed. This question needs to be more focused."
Anyway, here is one direction:
% sphere
%fun = #(x, y, z) x.^2 + y.^2 + z.^2 - 1;
% 3-torus
% http://metamathological.blogspot.com/2013/01/today-i-learned-how-to-plot-doughnut.html
fun = #(x, y, z) (x.^2 + (y - 2).^2 - 1) .* ((x - sqrt(3)).^2 + (y + 1).^2 - 1) .* ((x + sqrt(3)).^2 + (y + 1).^2 - 1) - 0.0015*(x.^2 + y.^2 + 7*z.^2).^5;
% 4-torus
%fun = #(x, y, z) ((x - 2).^2 + (y - 2).^2 - 1) .* ((x - 2).^2 + (y + 2).^2 - 1) .* ((x + 2).^2 + (y - 2).^2 - 1) .* ((x + 2).^2 + (y + 2).^2 - 1) - 0.02*(x.^2 + y.^2 + 7*z.^2).^5;
res = 0.1;
r = 8;
x = -r:res:r;
y = -r:res:r;
z = -r:res:r;
[X, Y, Z] = meshgrid(x, y, z);
V = fun(X,Y,Z); % volumetric function (e.g. distance function)
I = isosurface(x ,y , z, V, 0); % extract level set as a mesh
p = patch(I);
isonormals(X, Y, Z, V, p);
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1 1 1])
view(3);
axis tight;
camlight;
lighting gouraud;
I am trying to code Horner’s Method for Polynomial Evaluation but for some reason its not working for me and I'm not sure where I am getting it wrong.
These are the data I have:
nodes = [-2, -1, 1]
x = 2
c (coefficients) = [-3, 3, -1]
The code I have so far is:
function y = horner(x, nodes, c)
n = length(c);
y = c(1);
for i = 2:n
y = y * ((x - nodes(i - 1)) + c(i));
end
end
I am supposed to end up with a polynomial such as (−1)·(x+2)(x+1)+3·(x+2)−3·1 and if x =2 then I am supposed to get -3. But for some reason I don't know where I am going wrong.
Edit:
So I changed my code. I think it works but I am not sure:
function y = horner(x, nodes, c)
n = length(c);
y = c(n);
for k = n-1:-1:1
y = c(k) + y * (x - nodes((n - k) + 1));
end
end
This works:
function y = horner(x, nodes, c)
n = length(c);
y = 0;
for i = 1:n % We iterate over `c`
tmp = c(i);
for j = 1:i-1 % We iterate over the relevant elements of `nodes`
tmp *= x - nodes(j); % We multiply `c(i) * (x - nodes(1)) * (x -nodes(2)) * (x- nodes(3)) * ... * (x - nodes(i -1))
end
y += tmp; % We added each product to y
end
% Here `y` is as following:
% c(1) + c(2) * (x - nodes(1)) + c(3) * (x - nodes(1)) * (x - nodes(2)) + ... + c(n) * (x - nodes(1)) * ... * (x - nodes(n - 1))
end
(I'm sorry this isn't python but I don't know python)
In the case where we didn't have nodes, horner's method works like this:
p = c[n]
for i=n-1 .. 1
p = x*p + c[i]
for example for a quadratic (with coeffs a,b,c) this is
p = x*(x*a+b)+c
Note that if your language supports fma
fma(x,y,x) = x*y+z
then horner's method can be written
p = c[n]
for i=n-1 .. 1
p = fma( x, p, c[i])
When you do have nodes, the change is simple:
p = c[n]
for i=n-1 .. 1
p = (x-nodes[i])*p + c[i]
Or, using fma
p = c[n]
for i=n-1 .. 1
p = fma( (x-nodes[i]), p, c[i])
For the quadratic above this leads to
p = (x-nodes[1]*((x-nodes[2])*a+b)+c
I want to calculate the integral of the following function using int in MATLAB. I tried the following way, but it always shows the error Input arguments must be 'double':
syms x
int(exp(x) .* 1000 .* square(1000 .* (x -1)))
or
syms x
int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000, 1000)
or
syms x
int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000.0, 1000.0)
My function definitions are:
function y = step(t)
y = (1 + sign(t))/2;
end
function y = square(t)
y = step(t + 0.5) - step(t - 0.5);
end
You need to declare your functions to be symbolic:
syms square
syms step2
Why step2? Because step is an already defined function of a common toolbox.
MWE
function test
syms x z
syms square
syms step2
z1 = int(exp(x) .* 1000 .* square(1000 .* (x -1)))
z2 = int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000, 1000)
z3 = int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000.0, 1000.0)
end
function y=step2(t)
y = (1 + sign(t))/2;
end
function y = square(t)
y = step2(t + 0.5) - step2(t - 0.5);
end
z1 = 500*exp(x)*(sign(1000*x - 1999/2) - sign(1000*x - 2001/2))
z2 = 1000*exp(1999/2000)*(exp(1/1000) - 1)
z3 = 1000*exp(1999/2000)*(exp(1/1000) - 1)
in scala, given the integers d & x, I would have a boolean expression which should be true if and only if y = (x^2 - 1) / d^2 is a square.
I tried this:
(Math.sqrt((x * x - 1) / (d * d)).toInt * Math.sqrt((x * x - 1) / (d * d)).toInt == ((x * x - 1) / (d * d)))
but the 3-tuple (x = 2, d = <all values tested>, y = 0.0) seems to be always an answer of my problem, which is obviously wrong.
I think my error comes from the rounding made: if x=2, d=4 (for example) then x * x - 1 == 3 and d * d == 16 so the division leads to 0.
do you know what is the good expression?
if n is a round square, then Math.sqrt(n).toInt == Math.sqrt(n). In your case:
(Math.sqrt((x * x - 1) / (d * d)).toInt == Math.sqrt((x * x - 1) / (d * d)))
But before doing that, you need to make sure that x and d are doubles.
Try in REPL:
scala> val x = 1
scala> val d = 3
scala> x/d
A Int divided by an Int will result the rounded Int, so you are applying sqrt to zero.
Also due to float point arithmetic, you may want to compare like this instead:
(Math.sqrt((x * x - 1) / (d * d)).toInt - Math.sqrt((x * x - 1) / (d * d))) <= ZERO
where ZERO is replaced by a really small number like 0.00001
Because this is integer division, you are checking whether ((x*x-1)/(d*d)).toInt is a perfect square. You can convert everything to doubles first, but if you want to stay in the realm of integers, check that the division should result in an integer:
( x*x-1 % d*d == 0 ) && Math.sqrt(y).toInt == Math.sqrt(y)
This is a portion of my code:-
t = -4 : 0.01 :4;
f = inline('(-1.5*t+1) .* ((t>-3)&(t<0)) + (1.5*t+1) .* ((t>0)&(t<3))');
plot(t, f(t), 'r','linewidth', 2);
grid on;
Here I am getting the value of f(0) = 0
>> f(0)
ans =
0
I want to plot the function with f(0) = 3
For that I tried this
f = inline('(-1.5*t) .* ((t>-3)&(t<0)) + (3) .* (t = 0) +(t) .* ((t>0)&(t<3))');
But I am getting an error when I use the same plot command.
plot(t, f(t), 'r','linewidth', 2);
Can anybody suggest the correct method.?
The error is because you have (t = 0) but you probably wanted (t == 0)
i.e.
f = inline('(-1.5*t) .* ((t>-3)&(t<0)) + (3) .* (t == 0) +(t) .* ((t>0)&(t<3))');
now f(0) gives 3
It is easy...the mistake is
t == 0 not t= 0