Integration inside a triangle - matlab

I'm looking for a matlab command that integrates a known function inside a triangle (which is known by his nodes).
I have 3 coordinates (x1,y1) (x2,y2) (x3,y3) that define a triangle and I know that inside the triangle there exist a known function f(x,y). I'm looking for a command that computes the integral of f over the triangle.

Use integral2 command in Matlab. The first example in the Documentation will cover your case. To get one side of your triangle parallel to the x-axis you can multiply your triangle with a rotation matrix and then "move" it on the x-axis.
Note that the integral is not influenced this transformations if your perform your substitutions correctly.
This transformations can make it easier to write your code but they're not necessary to use integral2.

Related

Generate tubes with non-circular cross-sections in MuPad

Using plot::tube function in MuPAD, I can generate tubes with varying spine but the cross-section is always a circle, although the radius can vary along the spine. For instance:
plot::Tube([cos(t), sin(t), 0], 0.4 + 0.3*sin(t)*cos(t),t = -0.5*PI..0.5*PI,Mesh=[60,12]):
I want to plot tubes with (non-circular) elliptical cross-sections. Is there a way to achieve this in MuPAD?
P.S. I can generate tubes with arbitrary cross-sections using MATLAV, so please avoid including answers that use pure MATLAB commands.
As for your first question whether this can be done with plot::Tube: I don't think so. Consider the help of this function (emphasis mine):
plot::Tube creates generalized tubular plots, known as "canal
surfaces", with special cases known as "tube surface", "pipe surface"
or "tubular surfaces."
Intuitively, canal surfaces are space curves with thickness. More formally, a canal surface plot::Tube([x(t), y(t), z(t)], r(t), t = t_min..t_max) is the envelope of spheres with center [x(t), y(t), z(t)] and radius r(t), i.e., the thickness of the curve can vary with
the curve parameter t
The fact that a tube is defined as an envelope of a collection of spheres suggests that their cross-section is inherently circular.
I'm not familiar with MuPAD, so I don't know the elegant solution to your problem. For your simple example of a semi-circular base line I could put together this kludgy solution using the low-level plot::Surface, manually constructing the surface:
plot(
plot::Surface(
matrix([cos(t),sin(t),0])
+ numeric::rotationMatrix(t,[0,0,1]) * matrix([(0.2+0.1*sin(2*t))*cos(u),0,(0.2+0.1*cos(2*t))*sin(u)]),
t = -PI/2..PI/2,
u = 0..2*PI,
Mesh = [30,30]),
Scaling=Constrained)
Here t stands for the angular parameter along the semi-circular base line (toroidal direction), and u is the angle along the cross-section (poloidal direction). A given cross-section at angle t looks like this:
[(0.2+0.1*sin(2*t))*cos(u), 0, (0.2+0.1*cos(2*t))*sin(u)]
I.e. the semimajor axes are (0.2+0.1*sin(2*t)) and (0.2+0.1*cos(2*t)) along the cross-section of the tube.
You can see that I used a rotation matrix around the z axis to construct the surface. This made heavy use of the fact that the base line is a semi-circle. However, in general cases it should be possible to compute the derivative of your parameterized base line (with respect to t), and compute the necessary rotation angle (with parameter t) from that.

Creating patterns in matlab / octave to show Moiré patterns

How can I create radial images like this (see images below)
My goal is to control the number of radial arms, thinkness, along with the angle they are created. I'm trying to create patterns that will show me different Moiré patterns when overlapped and turned / animated in octave / matlab.
PS: I'm using octave 3.8.1
I've tried the code here but it doesn't give me the fine tuning all of the following parameters, of radial arm amount, angle, and thickness. Also the image package is needed which I'm trying to avoid.
http://www.mathworks.com/matlabcentral/answers/uploaded_files/20287/moire_pattern.m
As I see it the two approaches which would be worth investigating first are equations and patches.
You could for instance generate a generic equation for an arm with parameters to control the rotation angle and the shape of the curve. You could then plot that at each of a given number of rotation angles, with varying linewidths (a plot property not an equation parameter). Your equation would probably not look pretty as you'd be best off specifying it parametrically (in terms of a third variable) or in polar coordinates, and then translating it to cartesian for the plot commands.
With patches you'd be computing the outline of the arm (as opposed to the centreline) and would probably find it convenient to generate the patch for one arm and then transform it for each rotation. This would be a one-liner with the appropriate rotational transform matrix, and the expression you use to generate the arm wouldn't need to be nearly so complex as it wouldn't need to handle the rotation. A quadratic might even do at a push.
Another advantage of patches is that, having generated an arm and rotated it around, you could also flip it and generate the figure with the opposite sense for very little extra code.

finding gradient/curvature of surface defined by arbitrary, non-grid-spaced points

So I have a 3 dimensional matrix of points that (presumably) define a surface. For my purposes, X and Y can be random values but when plotted along with their Z coordinates, they will define some undulating surface. I'd like to measure the local curvatures of said surface, and in order to do that, I need to be able to find the gradient of said surface, at which point calculating the curvature is trivial.
I have not yet found an implementation of how to measure this curvature that doesn't make use of Matlab's gradient function. The problem with Matlab's gradient function is that it assumes that the points are in some sort of order, similar to diff(X). This would suffice if my points were spaced along a grid, which is not necessarily the case.
One possible solution to measuring the gradient is to give in and assign each point to a discrete coordinate in a grid in the XY plane, thus overcoming this issue. However, this solution seems somewhat inelegant and was curious to see if anyone had suggestions. Thanks!
You can use griddata to interpolate from your scattered data points to grid spaced points and then calculate the gradient.

Projecting Conical Helix on Cone in Matlab?

Suppose you have f(x)=x-floor(x).
By this, you can generate the grooves by gluing the top side and the
bottom side together and then squeezing the left to zero -- now you
have a conical helix: the line spins around the cone until it hits the
bottom. You already have one form of the equations for the conical
helix namely x=a*cos(a); y=a*sin(a); z=a. Now like
here:
How can you project the conical helix on the cone in Matlab?
I'd approach your problem without using plot3, instead I'd use meshgrid and sinc. Note that sinc is a matlab built in functions that just do sin(x)./x, for example:
So in 1-D, if I understand you correctly you want to "project" sinc(x) on sqrt(x.^2). The problem with your question is that you mention projection with the dot product, but a dot product reduces the dimensionality, so a dot product of two vectors gives a scalar, and of two 2D surfaces - a vector, so I don't understand what you mean. From the 2-D plot you added I interpreted the question as to "dress" one function with the other, like in addition...
Here's the implementation:
N=64;
[x y]=meshgrid(linspace(-3*pi,3*pi,N),linspace(-3*pi,3*pi,N));
t=sqrt(x.^2+y.^2);
f=t+2*sinc(t);
subplot(1,2,1)
mesh(x,y,f) ; axis vis3d
subplot(1,2,2)
mesh(x,y,f)
view(0,0) ; axis square
colormap bone
The factor 2 in the sinc was placed for better visualization of the fluctuations of the sinc.

Mesh Generation in MATLAB

Is there any subroutine, in MATLAB, that takes in a list of points, and return me a good mesh that I can use to show to my colleagues, such as this?
Actually, all I need is just a simple 2D mesh generator that takes in a series of X, Y coordinates (that defines the boundary of the area), and give me back a list of elements that can mesh that area well. I can do the rest by using MATLAB command to interpolate the Z value.
Edit : I am not interested to use MATLAB to produce the above looking plot. I am interested in using a MATLAB library to obtain a list of elements so that when I plot those element myself (not in MATLAB itself; but in my own C# program), I can obtain this meshed surface.
PS: I know there is this DistMesh, but I am looking for something simpler - something built-in direct in MATLAB perhaps. And no, meshgrid is not mesh generation.
It sounds like you want to create a finite element mesh, starting with a set of points defining a boundary of a region and then generating a triangular mesh that creates more points within that region. I don't think there's a "simple" solution for this problem.
The closest "built-in" solution would probably be the Partial Differential Equation Toolbox, specifically some of the Geometry Algorithms like INITMESH and REFINEMESH.
The link you gave to DistMesh appears to be another good solution. There are also a few submissions on the MathWorks File Exchange that you could take a look at:
MESH2D by Darren Engwirda
Finite Element Toolbox 2.1 by Rasmus Anthin
That picture looks exactly like the one from the griddata documentation. The example in there looks like what you want.
SFTOOL will easily make the picture that you show.
A thin-plate spline, e.g., TPAPS, should also do the job.
I think the user-created 'gridfit' is the best I've come across for a single surface, much better/prettier than griddata.
Mesh generation as in Delaunay Triangulation + Steiner Points? There is a builtin Delaunay function in MATLAB.
If your surface is the z=f(x,y) form you can use:
http://www.advancedmcode.org/how-to-plot-a-coloured-surface-from-3d-scatter.html
If your surface is concave look for surface reconstruction on the same website.