**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

78 views (last 30 days)

Show older comments

##### 2 Comments

Gavin Seddon
on 15 Nov 2017

### 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

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

Star Strider
on 12 Apr 2017

As always, my pleasure.

Gavin Seddon
on 13 Apr 2017

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

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

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

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

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

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

Star Strider
on 27 Apr 2017

Gavin Seddon
on 29 Apr 2017

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

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

Star Strider
on 4 May 2017

As always, my pleasure.

Gavin Seddon
on 8 May 2017

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

Star Strider
on 11 May 2017

Gavin Seddon
on 15 May 2017

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

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

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

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

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

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

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

Star Strider
on 17 May 2017

My pleasure.

I will help as I can.

Gavin Seddon
on 18 May 2017

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

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

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

Star Strider
on 3 Jun 2017

My pleasure.

Gavin Seddon
on 5 Jun 2017

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

Star Strider
on 15 Nov 2017

As always, my pleasure.

Gavin Seddon
on 6 Mar 2018

Star Strider
on 6 Mar 2018

As always, my pleasure.

Gavin Seddon
on 6 Mar 2018

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

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

Star Strider
on 21 Mar 2018

My pleasure.

### More Answers (0)

### See Also

### Categories

### 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 (한국어)