3-D Vector Fields for Modeling - matlab

I hope this isn't too general of a question, but I'm having an issue with some syntax in this 3D vector field example provided on Mathworks' website in addition to actually just understanding how to make the 3d vector fields.
I'm trying to create a very robust 3D vector field to model wind patterns based on current NOAA information for a geographic region to predict (or simulate) a path in which a balloon system / parachute system would / should drift on ascent and descent.
In saying that, I'm just starting with the 3D vector fields and the same could be said for 2D vector fields, but I can't seem to find a great resource for the 3D ones.
field := plot::VectorField3d([1, sin(x) + cos(y), sin(z)],
x = 0..6, y = 0..2.5, z = 0..5,
Mesh = [7, 7, 7]):
plot(field):
This is one of the examples, but I'm not familiar with the usage of ".." in succession or the stacking that they seem to have done with the lines. I've tried copying this into a new ".m" file that I created and running it, but I get: "Unexpected MATLAB expression". I've tried guessing at the notation by switching ".." to ":" and the ending ":" to a ";" , but I didn't get anywhere.
If you need me to clarify, let me know, and thanks for reading / helping in advance!

0..6 stands for a range - see http://www.mathworks.com/help/symbolic/mupad_ref/uname.html
To run your example, type mupad in the command window and paste the example in the mupad editor.

Related

Equivalent for (:) for slices of higher dimension matrices

Context
You have a 3D variable A
rng('default')
A = randi(100,5,7,3);
Problem
You want to get a column vector with all the values of a given slice along the third dimension of A, e.g.
Tmp = A(:,:,2);
out = Tmp(:);
Question
Is there a built in way to do this directly without having to use a temporary variable or a function (i.e. with a combination of brackets, ...)? The closest to what I look for I have found yet would be
out = reshape(A(:,:,2),[],1)
Which I find a bit "heavy". I was looking for something like (A(:,:,2))(:) but it doesn't work in MATLAB.
The fact that they added the input all in functions like any would suggest that there is not but I figured I'd still ask

How to calculate Gradient in matlab?

I am working on pedestrian step detection (acceleration). I want to calculate statistical features from my filtered signal. I have already calculated some and now I want to calculate gradient.
My data is of 1x37205 double. I calculated features using for loop with moving window size=2samples and 50% overlap of previous window. Below I am attaching the code I tried to calculate the gradient.
I am not sure if it is the right way to calculate or not? In addition, I am also unable to understand that what is the purpose to use gradient, how it can be useful for step detection and how to work with gradient? Could some one guide me or provide any code help in matlab?
%%Here M is mean and V is variance i already calculated from filtered data
G = zeros(length(window:length(M)), 2);
for i = window:length(M)
temp = gradient(M(i+1-window:i),V(i+1-window:i));
G(i, 1) = temp(2, 1); % "c1"
G(i, 2) = temp(2, 1); % "c2"
end
One of the best features of Matlab is its documentation. If you are unfamiliar on how to get specific function documentation, enter the following in the command line:
doc functionName
Alternatively, for 'brief' documentation that displays in the command line, you can enter:
help functionName
Also see the documentation link here.
Your question is worded poorly, so I will summarize what I understand and answer accordingly:
You have a step detection data (1*37205) double, let us call it stepSignal
stepSignal is position data
You want your window to be 2 steps with 50% overlap. This is the default behavior for the gradient function.
You do not need a "for" loop to achieve your goal. According to the documentation, "gradient" can take one input.
See the code below, and if need be add clarifications to the original question.
%% Assume that stepSignal is already imported into the workspace
velocity = gradient(stepSignal);
One last note, when you give "gradient" two inputs, it automatically assumes the second input is a uniform spacing value.

Why does nothing work in my Matlab plot?

I am trying to plot with Matlab. In particular, I try with numerous online source but none of them work.
Here is my problem, I am trying to plot the expression: y=2*(x-1)/(x-4)Kb/L, and I am interested in the range of x between 0 and 1.
K=40;
b=20;
L=0.5;
x=linspace(0,1,1000);
y=2*(x-1)/(x-4)*K*b/L;
but it returns:
y=275.01
I know linspace isn't the proper way to plot. How can I plot this function? I want to keep the K,b,L declaration because I might change them latter.
y=2*(x-1)./(x-4)*K*b/L; you should use ./ replace /
Like hzy199411 said, you should use the "." operation.
I would suggest that you type "help ." at a MATLAB command prompt. MATLAB will respond with a large index of results but look for the section on "Arithmetic Operators".
You may also try the command "doc arith" but I think the "help ." is more helpful because at least in MATLAB 2013 it verbosely lists more "dot" operators.
In short several arithmetic operators prefixed with '.' ("Dot") are "Element-by-Element" operations and as such they operate on each index of the array/matrix.
For example if you had an array s=1:20 and you performed the operation s/s you would get ans = 1, where as if you did s./s you would get an array of 1's with the same length as 's'.
I guess that you are a new matlab user :). The program is in general ok, but you should think of some things. First,
linspace is not a plotting function. The function is useful though. With your syntax it creates a vector of length 1000 with range [0,1]. For plotting, type:
plot(x,y);
Linecolor and style can be set as
plot(x,y,'r-.');
For predefined colors (here 'r-.' means a red dotted line). There are also some additional properties that can be found be checking the online help of plot.
Also as the others say, if you want to operate on each element in the vector, use ./. The / is a matrix operator.

Suppress kinks in a plot matlab

I have a csv file which contains data like below:[1st row is header]
Element,State,Time
Water,Solid,1
Water,Solid,2
Water,Solid,3
Water,Solid,4
Water,Solid,5
Water,Solid,2
Water,Solid,3
Water,Solid,4
Water,Solid,5
Water,Solid,6
Water,Solid,7
Water,Solid,8
Water,Solid,7
Water,Solid,6
Water,Solid,5
Water,Solid,4
Water,Solid,3
The similar pattern is repeated for State: "Solid" replaced with Liquid and Gas.
And moreover the Element "Water" can be replaced by some other element too.
Time as Integer's are in seconds (to simplify) but can be any real number.
Additionally there might by some comment line starting with # in between the file.
Problem Statement: I want to eliminate the first dip in Time values and smooth out using some quadratic or cubic or polynomial interpolation [please notice the first change from 5->2 --->8. I want to replace these numbers to intermediate values giving a gradual/smooth increase from 5--->8].
And I wish this to be done for all the combinations of Elements and States.
Is this possible through some sort of coding in Matlab etc ?
Any Pointers will be helpful !!
Thanks in advance :)
You can use the interp1 function for 1D-interpolation. The syntax is
yi = interp1(x,y,xi,method)
where x are your original coordinates, y are your original values, xi are the coordinates at which you want the values to be interpolated at and yi are the interpolated values. method can be 'spline' (cubic spline interpolation), 'pchip' (piece-wise Hermite), 'cubic' (cubic polynomial) and others (see the documentation for details).
You have alot of options here, it really depends on the nature of your data, but I would start of with a simple moving average (MA) filter (which replaces each data point with the average of the neighboring data points), and see were that takes me. It's easy to implement, and fine-tuning the MA-span a couple of times on some sample data is usually enough.
http://www.mathworks.se/help/curvefit/smoothing-data.html
I would not try to fit a polynomial to the entire data set unless I really needed to compress it, (but to do so you can use the polyfit function).

Problems finding the intersecting point of circle

Recently I posted another thread and the answers were really helpful. From this reference I tried to write the code for two intersecting circle.
I think I did something wrong with my code and the result it's giving me the mid point of the connecting line of the intersection points. I have no clue what's wrong with my code.
I'm finding it difficult to handle the loop. I need the actual length of the vectors but if I wrote it to the loop, for i+1 it is showing me the error (which is very natural as I don't find any '<' operator :(()
Here's my code -
Xs = [150,130];% x coordinates of center of circles
Ys = [100,250];% y coordinates of center of circles
Rs = [100,70];% radius of circles
C=Imread('spinks_map.png');
figure, Imshow(C)
hold on;
simple_draw_circle( Xs, Ys, Rs);
for i=1:length(Xs)-1
dsq(i) = ((Xs(i+1)- Xs(i))^2) + ((Ys(i+1) - Ys(i))^2);
K(i)= (sqrt((((Rs(i)+Rs(i+1))^2)-dsq(i))-(dsq(i)-(Rs(i)-Rs(i+1))^2)))/4;
x(i) = ((Xs(i+1)+Xs(i))/2) + ((Xs(i+1)-Xs(i))*(Rs(i)^2-Rs(i+1)^2)/dsq(i))/2 - 2*(Ys(i+1)-Ys(i))*(K(i)/dsq(i));
y(i) = ((Ys(i+1)+Ys(i))/2) + ((Ys(i+1)-Ys(i))*(Rs(i)^2-Rs(i+1)^2)/dsq(i))/2 +(-2)*(Xs(i+1)-Xs(i))*(K(i)/dsq(i));
end
scatter(x(1),y(1),'filled');
You have an error in your use of Heron's formula. Look at the link you posted that gives the calculation for K. There should be a product between the two terms inside the square root, not a difference:
K(i)= (sqrt((((Rs(i)+Rs(i+1))^2)-dsq(i))*(dsq(i)-(Rs(i)-Rs(i+1))^2)))/4;
Also, with regards to your second question. I've found it to be a best practice for me if I explicitly declare order of operations when using an expression with ":".
For your loop, using the appropriate choice of either
for i=(1:length(Xs))-1
or
for i=1:(length(Xs)-1)
can cut down on unexpected behaviors during indexing. This was the issue as I understood it from your post. If it was something else, then post the error text that Matlab spits up and I'll take a look at it.
There is a "<" operator in Matlab. If you're trying to use it in the style of the C equivalent for loop:
for(iter=0;iter++,iter<2){
something();
}
then look into the syntax for the Matlab while loop.
Best of luck,
Andrew