You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Creating phase plot from initial data
8 views (last 30 days)
Show older comments
I have a data table with no associated function, is it possible to generate the vector field and phase diagram from this? GS.
2 Comments
Gavin Seddon
on 15 Nov 2017
You are welcome, I have tried the arc tangent with my table and it appears I should define two points and calculate the angle. Also I cannot calculate the gradient since it complains that my matrix is not equal, Possibly because I am using fxy (multivariate). Nevertheless I have been of data now to complete the job. GS.
Accepted Answer
Star Strider
on 30 Mar 2017
Use the gradient function to create the derivatives.
See if this works with your data:
x = sin(0:0.5:2*pi*2); % Create Data
y = cos(0:0.5:2*pi*2); % Create Data
u = gradient(x); % Calculate ‘x’ Derivative
v = gradient(y); % Calculate ‘y’ Derivative
figure(1)
quiver(x, y, u, v)
grid
axis equal
78 Comments
Gavin Seddon
on 31 Mar 2017
Hello Star Strider thank you by doing this is it attempting to locate a 'ubiquitous' function? Also I will investigate Quiver immediately however I will attempt your solution on Monday. GS.
Star Strider
on 31 Mar 2017
My pleasure.
My code assumes that your data are reasonably continuous and have very low noise. (Taking the derivative significantly amplifies noise, so doing some empirical curve-fitting, perhaps with the polyfit and polyval functions may be necessary.) All of these are core MATLAB functions.
Have a restful weekend!
Gavin Seddon
on 10 Apr 2017
Edited: Gavin Seddon
on 10 Apr 2017
hello Star Strider
I have isolated a curve which I am repeating your process.
I have the fx and it appears fy is its derivative.
Is this true and if so will you explain why this is
the case since I have never considered this? GS.
Star Strider
on 10 Apr 2017
I have no idea what your data are, so I cannot comment on them.
If you calculated ‘fy’ by using the gradient function on ‘fx’, ‘fy’ would be the numerical derivative of ‘fx’.
Gavin Seddon
on 12 Apr 2017
Thank you, I was unsure if fy was indeed the derivative and you have confirmed this (which was my question). I never considered this before. GS.
Star Strider
on 12 Apr 2017
As always, my pleasure.
Gavin Seddon
on 13 Apr 2017
Apologies Star Strider My Data Gives the Error Error using quiver (line 44) DOUBLE cannot convert the input expression into a double array. Does this mean my data is incompatible? GS.
Star Strider
on 13 Apr 2017
‘Does this mean my data is incompatible?’
Not necessarily. It depends where you got it (for example, a symbolic calculation) and the data type or array type (table, structure, cell, etc.). If it is a symbolic calculation with a symbolic variable, you first must remove the symbolic variable. (Sometimes, using vpa or vpasolve will work.) For the other array types, you will have to extract each vector from them as a double array in order to use the plot and quiver functions.
What you need to do depends on what you are starting with.
Gavin Seddon
on 13 Apr 2017
Thank You it is a table;
happy Easter. GS.
Star Strider
on 13 Apr 2017
My pleasure.
You too!
You can probably use the table2array function. This creates a matrix from your table, so you can access your data as you would normally access the data in a matrix. Choose the appropriate columns (or rows).
It converts only numerical data, not categorical data or column headers. From your description of what you want to do, I doubt this will be a problem in your application.
See the documentation for the function. It has links to related functions that may be more appropriate for what you want to do.
Gavin Seddon
on 18 Apr 2017
Hello Star Strider, these commands as you said fit my data better. May I also use a higher function (I used primary for simplicity I would use cubic)? Your suggestion for using matrices has made me look at iterative mapping was this your idea? Thank you. GS.
Star Strider
on 18 Apr 2017
My pleasure.
I would not use a polynomial fit of higher order than 7, and usually 3 will be adequate.
My impression is that your data (originally a table, extracted to a matrix) are already a matrix. I am not certain what you mean by ‘iterative mapping’. You can fit matrix variables with a number of different approaches (depending on what you want to do), and there are several iterative nonlinear function fitting functions available in MATLAB and various Toolboxes.
Gavin Seddon
on 19 Apr 2017
Hello again, I was correct you have perceived that my data is non-linear (Lorentz used iterative mapping). For this reason I would rather avoid 'tailoring' it or using high polynomials. I will try both polyfit and a cubic function. Will you suggest toolboxes for non-linear plotting and perhaps chaos investigation? Finally, if I encounter any issues with polyfit I will put that on a new thread. Thanks again. GS.
Star Strider
on 19 Apr 2017
My pleasure.
There are several nonlinear parameter estimation (and related) functions available. The Statistics and Machine Learning Toolbox nlinfit, and the Optimization Toolbox lsqcurvefit functions are options. I don’t have the Curve Fitting Toolbox, so I can’t comment on it.
I’ve not done anything with chaos theory in a long time, so I’m not familiar with using MATLAB for it. The File Exchange may have contributions that would do what you want.
The polyfit function will only fit vector data, nlinfit will fit matrix independent and vector dependent variables, and lsqcurvefit will fit matrix independent and dependent variables.
Gavin Seddon
on 21 Apr 2017
Hello star Strider The Cubic Function failed with the same error so I am looking into the non-linear nature of my work. Thank you it appears lsqcurvefit maybe appropriate. I will be absent until the end of next week at which time I will submit a new question since I cannot see how to create the initial function for use with lsqcurvefit. Your initial answer was perfect. GS.
Star Strider
on 21 Apr 2017
Thank you!
In your new Question, please describe your data (and include it as a file if possible) and what you want to do, and post the equations you are fitting (as a posted image of them as well as code), since not everyone who can help you may be familiar with it. That ‘Lorenz used iterative mapping’ to solve it means that it may not be among my areas of expertise, although if I have an understanding of the approach you want to take, and if coding is the only problem, my knowing the details of what you are doing may not be necessary.
I am certain at least one of us here will be able to help you if we have a reasonable understanding of what you want to do.
Gavin Seddon
on 21 Apr 2017
Hello again, that is wonderful. I have measured a change in position of a molecule within a cell (recorded in a table). When plotted it is clearly sinusoital suggesting rotation of the body. Hence I attempted a vector plot (you know the rest here). The more I consider the curve it appears more non-linear. Is it possible to use lsqcurvefit to derive a function? GS.
Star Strider
on 21 Apr 2017
Thank you!
The lsqcurvefit function will fit data to a function, but will not derive one for you.
The Symbolic Math Toolbox would help with the symbolic calculations of the model function you want to derive, and will even generate MATLAB code from it (using the matlabFunction function) that you can use as your objective function for lsqcurvefit. The Symbolic Math Toolbox has its limits, and we can help you deal with them.
Gavin Seddon
on 23 Apr 2017
This is really good. I may have devised a method for determining molecular co variability which may be used with three-dimensional quiver. I must leave now. Thanks. GS.
Star Strider
on 23 Apr 2017
It would be terrific if the method you devised is sufficiently novel to be worthy of publication!
See you later!
Gavin Seddon
on 27 Apr 2017
Hello Star Strider, I will install the necessary toolbox and investigate this. No doubt Guy will need further help with this. GS.
Star Strider
on 27 Apr 2017
I will probably be able to help you with any programming problems. I’m not certain about the physics.
Gavin Seddon
on 29 Apr 2017
Hello again, it appears symfun will generate the symbolic function. Would this fit in my data better because prima facie it appears not to use my data? Will you directly further please? GS.
Star Strider
on 29 Apr 2017
The symfun function allows you to create a symbolic function, then manipulate it symbolically so that it does what you want.
To use it in your numeric MATLAB script, you then have to use the matlabFunction function to create an anonymous function or a function file from it (your decision, so choose whatever is most appropriate). You can then use that function in your numeric MATLAB code. (Print the anonymous function to the Command Window by removing the parenthesis from the end of the matlabFunction call, then copy it and paste it into your numeric calculation script.) Then you can call the function from your script, just as you would any other function.
Please read through the documentation on matlabFunction thoroughly. It has a number of options that can make your task significantly easier.
I don’t know what your symfun function is, so look through the documentation on the odeToVectorField function as well to see if it could be applicable. Use it before you call matlabFunction.
It is best not to do extensive or recursive calculations in the Symbolic Math Toolbox, unless you need its extended-precision calculation ability. It isn’t optimised for such calculations, and is extremely slow, when compared to doing the same calculations in non-symbolic code.
Gavin Seddon
on 3 May 2017
I have read your suggestions however the very basic problem of constructing a non-linear curve/function eludes me. I was wondering if * lsqcurvefit* with the basic fitting UI is the way ahead; followed by your procedure or perhaps * quiver* used? GS.
Star Strider
on 3 May 2017
I thought you already had your function and only need to express it as MATLAB code. I will do my best to help with that if you need it. (That may be the limit of my ability to help, since the physics of what you are doing may be outside my areas of expertise.)
The lsqcurvefit function will likely do what you want. It has several advantages, specifically the ability to fit matrix dependent variables, and the ability to constrain the parameter values. The disadvantages are that while parameter confidence intervals are possible (if I remember correctly), statistics on the fit (prediction confidence intervals) and some other statistics are not available. I am not familiar with the UI for it, since I use the script commands.
The quiver function is not difficult. It requires the (x,y) coordinates of the data, and the derivatives of the curve in the (x,y) directions to specify the arrows. An easy way to calculate those are with the gradient function.
Gavin Seddon
on 4 May 2017
Thank you Star Strider that is wonderful and please don't worry about the biophysics/biochemistry. GS.
Star Strider
on 4 May 2017
As always, my pleasure.
Gavin Seddon
on 8 May 2017
Hello star strider, I have given this issue plenty of thought over the weekend and I presume the way ahead will be: nlinfit which I hope will generate a better fit to my initial cubic function. Then in order to produce a less 'messy' function a symbolic function can be used employing symfun which is subsequently used to form the handle by matlabfunction then this can be imported into your protocol for the production of the vector field. However I have found it is better to use dIff for symbolic functions. No doubt during the work I will need help I hope this is okay? GS.
Star Strider
on 8 May 2017
If you are fitting matrix dependent variables, you must use lsqcurvefit. For vector dependent variables, both nlinfit and lsqcurvefit will work. (I still do not completely understand what you are doing.)
I do not understand using the diff function unless you want to create an ODE function to use with a numeric solver (for example ode45), or if you are fitting derivatives of data.
I will of course help as much as I am able.
Gavin Seddon
on 11 May 2017
Hello, when I add the variable using syms Matlab immediately evaluate the function and removes the constants. Is it possible within the variable to retain the constants within the function and proceed with the protocol? GS.
Star Strider
on 11 May 2017
The only thing I can think of is to comment-out the values of the constants. It won’t substitute them if it has nothing to substitute them for.
Gavin Seddon
on 15 May 2017
Hello Star Strider you were perfectly correct on the non-linear issues, it appears lsqcurvefit is the command to use. However it pays unnecessarily difficult to use and that I will continue with using symfun. GS.
Gavin Seddon
on 15 May 2017
Edited: Star Strider
on 15 May 2017
Hello again, I have processed the following, will you advise further sold the function becomes symbolic?
fs = symfun(x + y, [x y])
fs(x, y) = x + y
eval fs
fs(x, y) =
x + y
GS.
Star Strider
on 15 May 2017
Hi. ‘fs’ is a symbolic function as you have defined it.
You can convert it to an anonymous function to use in numeric code with the matlabFunction function.
Gavin Seddon
on 15 May 2017
Hello, my initial function fx was generated using a table of two columns labelled time and distance however my function uses x. I created fs using x and y according to the manual. Whilst I follow the Matlabfunction command how do I relate distance, time to the functions? This is probably obvious but I am missing a fundamental point. Thanks for your help. GS.
Star Strider
on 15 May 2017
You can name the function variables anything you want. (I would of course not add ‘time’ and ‘distance’ since the units are different.)
Is something like this what you want?
syms t d v
v(t,d) = d/t; % Calculate Velocity As Function Of Distance & Time
v = matlabFunction(v)
v =
function_handle with value:
@(t,d)d./t
Gavin Seddon
on 16 May 2017
did you need *matlabfunction* and by function_handle the actual function? I didn't understand the last part or is that he symbolic notation? GS.
Star Strider
on 16 May 2017
The matlabFunction function creates a function handle to the anonymous function code it creates. You can then call it as any other function, for example:
d = [0 1.0 1.5 2.5 4.0 4.5 4.1 2.0 1.0];
t = [1 1.5 2.0 2.5 2.7 3.0 3.8 4.0 4.5];
velocity = v(t,d);
I am a bit lost. I am not certain what you are asking. I want to help as much as I can.
Gavin Seddon
on 16 May 2017
This did not print your answer was slightly confusing, did you mean the matlabfunction command and then did you mean your final handle is symbolic? GS. My previous attempt was corrupted, sorry.
Star Strider
on 16 May 2017
No worries.
The matlabFunction function converts a symbolic function to an anonymous function (or a function file) to use in numeric (non-symbolic) MATLAB code.
Gavin Seddon
on 16 May 2017
Thank you for the understanding, I now have my original function fx which has the equation, a symbolic function fs, which I created previous but I don't know how this relates to fx. If successful if I could relate these I assume fx = fun; fm = matlabfunction (fx) am I preceding correctly. GS.
Star Strider
on 16 May 2017
My pleasure.
Your code is correct if ‘fx’ is symbolic.
If one is a function of the other, and they both begin as symbolic functions, consider using the Symbolic Math Toolbox compose function.
Gavin Seddon
on 17 May 2017
Hello, are you suggesting; hybridising my symbolic function and the initial function fx to produce a new function which may then be passed to matlabfunction to enable the formation of the anonymous function? The compose command appears difficult (to me). Alternatively is it possible to transform fx into a symbolic function directly? Thanks. GS.
Star Strider
on 17 May 2017
I’m not certain where we’re starting from, here. If both ‘fs’ and ‘fx’ are symbolic functions, you can manipulate them in the Symbolic Math Toolbox and then use matlabFunction to convert them to anonymous functions to use in numerical calculations. (The Symbolic Math Toolbox can do one-off numerical calculations. It is extremely inefficient for iterative calculations.)
If you want to manipulate your anonymous function in the Symbolic Math Toolbox, you have to create it new as a symbolic function. (This is a relatively simple copy-paste of everything except the function handle designation. It may be necessary to remove the dot-operators for element-wise calculations.) You can use it as an anonymous function in your symbolic code, however since it exists as a function handle, and not a symbolic object, you cannot do much with it as an anonymous function in the Symbolic Math Toolbox.
To Illustrate —
syms x y
anon_fcn = @(x) x.^2 + 5*x - 2; % Anonymous Function
out = anon_fcn(y); % Substitutes ‘y’
% q1 = diff(anon_fcn); % Throws Error
sym_fcn = symfun(x.^2 + 5*x - 2, x); % Creates Symbolic Function
q2 = diff(sym_fcn); % Calculates Derivative
Gavin Seddon
on 17 May 2017
Hello yes to your initial suggestion however you mentioned 'manipulate them' using the symbolic toolbox. May We proceed from this please? Incidentally yes my function from the curve is both cubic and was defined as symbolic fx it was my mistake to talk of fs, sorry for the confusion. GS.
Gavin Seddon
on 17 May 2017
Hello my first investigation gave simplify and subs however it is clear how these may help. GS.
Star Strider
on 17 May 2017
I intended by ‘manipulate them’ that they must be symbolic objects. (It is not possible to use anonymous-function function-handles as symbolic objects in the sense that you can use them as anything other than anonymous functions. It is not possible to do anything else with them.)
You can apply all the appropriate Symbolic Math Toolbox functions to your symbolic objects.
No worries about the typographical error between ‘fx’ and ‘fs’. Thank you for sorting that though, because I thought they were different and that one was an anonymous function and the other a symbolic function.
Gavin Seddon
on 17 May 2017
Thank you, when it is convenient will you direct me. I will be absent from Sunday until the following Friday (that is why I am obsessive with this, I am assuming it will prove necessary for the partials plot and vectors). GS.
Star Strider
on 17 May 2017
My pleasure.
I will help as I can.
Gavin Seddon
on 18 May 2017
Thank you, considering this problem I fear Matlab indicated initially that it should not be expressed 2 dimensionally. I think we have given this appropriate consideration, I have simplified the function (it is still long). I will now produce the table listing the multivariable values and proceed with three-dimensional quiver. GS.
Star Strider
on 18 May 2017
My pleasure.
Be careful using gradient with a matrix, since it takes the derivatives across both rows and columns. I would use it on each vector separately to calculate the directional derivatives for quiver3, if that is what you want to do. See the documentation on gradient for details.
Gavin Seddon
on 19 May 2017
I have started the multivariable approach. I am using the partial differential toolbox. Should I treat my data in a similar fashion to your original suggestion? Also do you know of any additional approaches that will benefit this work? After tomorrow I will be absent so please do not assume my lack of interest. GS.
Star Strider
on 19 May 2017
I have the Partial Differential Equation Toolbox but I’ve not used it in several years.
If you have a differential equation, you will be able to calculate the derivatives from your differential equation function and the solved differential equation output. It may not be necessary to use the gradient function.
I’ll be here to help as I can.
Gavin Seddon
on 1 Jun 2017
Hello, I have created a multivariable table, it is possible to load its data to pdetool to use with a mesh and then generate a cubic equation? Thank you. GS.
Star Strider
on 1 Jun 2017
I have the Partial Differential Equation Toolbox, but haven’t used it in years. It would take me a while to come up to speed with it.
So my preliminary reply is for you to experiment with it.
Gavin Seddon
on 2 Jun 2017
Hello I have plotted the data using cftool it is explainable in that both residues rotating simultaneously producing a Unremarkable plane. However the resulting function is very complex. I am hoping the partial toolbox produces a simpler function. Considering its methods and I suspect it may be necessary plot the graph with Matlab and put this into pdetool. Thanks. GS.
Star Strider
on 3 Jun 2017
My pleasure.
Gavin Seddon
on 5 Jun 2017
Hello on close examination the plot is very acceptable and therefore I am proceeding with using cftool.
Star Strider
on 5 Jun 2017
Congratulations on finding the solution! I’d like to see a PDF of a paper you publish on this, because I’d like to understand what you’re doing.
I do not have the Curve Fitting Toolbox, so I cannot help you with its functions.
Gavin Seddon
on 6 Jun 2017
Edited: Stephen23
on 6 Jun 2017
Hello, indeed my pleasure. However as a final attempt at the quiver3, I have the function
fxy =
(8589*x)/10000 + (9749*y)/1000 + (1211*x*y)/1000 - (1077*x^2*y)/500 + (5469*x^2)/10000 - (2249*x^3)/2500 - (321*y^2)/50 + 1869/200
Matlab gives fxy which is z but the manual requires w which would make sense since I plotted XY and Z. Would the differential of my function give W? GS.
Star Strider
on 6 Jun 2017
The problem is that is an assignment, not a function. If you want to use it as a function, you construct is as:
fxy = @(x,y) (8589*x)/10000 + (9749*y)/1000 + (1211*x.*y)/1000 - (1077*x.^2.*y)/500 + (5469*x.^2)/10000 - (2249*x.^3)/2500 - (321*y.^2)/50 + 1869/200;
I have no idea how ‘W’ relates to ‘X’ and ‘Y’, so I cannot answer that.
If you want to calculate and plot the surface normals, this works:
fxy = @(x,y) (8589*x)/10000 + (9749*y)/1000 + (1211*x.*y)/1000 - (1077*x.^2.*y)/500 + (5469*x.^2)/10000 - (2249*x.^3)/2500 - (321*y.^2)/50 + 1869/200;
x = 0:9;
y = 1:9;
[X,Y] = meshgrid(x,y);
Z = fxy(X,Y);
[U,V,W] = surfnorm(Z);
figure(1)
mesh(X, Y, Z)
hold on
quiver3(Z, U, V, W, 0.01)
hold off
Gavin Seddon
on 7 Jun 2017
Hello Star Strider
Thank You for this. It is terrific. I will send the results but it may take a little time. GS.
Star Strider
on 7 Jun 2017
My pleasure.
I understand.
Gavin Seddon
on 14 Nov 2017
Hello the derivative gives the magnitude, is it possible to quantify the direction? GS.
Star Strider
on 14 Nov 2017
I am not certain what you want.
The atan2 or atan2d functions will return an angle. I would do something like this:
angl = atan2(dy, dx);
Gavin Seddon
on 15 Nov 2017
Hello, I only want to give value for the position for the vector. This appears splendid. Another part of the multivariable treatment would be to calculate the initial gradient. This will be negative and complement my molecular motion figure in that the direction will be negative. Confusing but the article will make sense. Thank you. GS.
Star Strider
on 15 Nov 2017
As always, my pleasure.
Gavin Seddon
on 6 Mar 2018
Hello Star Strider to analyse this further. If the directional derivative is divided by its magnitude I will get the unit vector. Is it possible to create a figure of this arrow? This may then be superimposed upon a grid for analytic geometry. This will enable me to calculate the angle with the adjacent (X (time)). I may then give a direction quantitatively.? many thanks. GS.
Star Strider
on 6 Mar 2018
As always, my pleasure.
Gavin Seddon
on 6 Mar 2018
Hello, yes this looks perfect. It appears I do not need to transpose the arrow to extract data. At first look I cannot see how to place the arrow into a new figure, will it allow me to do this? Yet again a perfect answer. Thank you. GS.
Star Strider
on 6 Mar 2018
As always, my pleasure.
Gavin Seddon
on 6 Mar 2018
As always thank you, this will be tomorrow. Fatigue. GS.
Star Strider
on 6 Mar 2018
My pleasure.
Understood.
Gavin Seddon
on 7 Mar 2018
Hello, initially is the direction calculated from the two dependent variables? The Y is expected however Z is e 10; is this a product? Also is the direction a consequence of their interaction? GS.
Star Strider
on 7 Mar 2018
Edited: Star Strider
on 7 Mar 2018
I am not certain what you are referring to.
For an illustration of how quiver works (in this instance quiver3), see: Draw Triangle using quiver3 command in Matlab (link)
—————————
EDIT —
I have no idea what ‘Z’ is or how you calculated it. I have no explanation for its magnitude being 1E+10. If you provide more details, I will do my best to explain it.
Gavin Seddon
on 21 Mar 2018
Hello, I have decided the best way forward is to draw a labelled triangle. Then using my derivative as the absolute hypotenuse Value, then the angle cos = adjacent/hypotenuse. Thank you. GS.
Star Strider
on 21 Mar 2018
My pleasure.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)