Take data in, fit them to poly, calculate derivative at arbitrary cross points

7 views (last 30 days)
Hi All,
first post here. I am new to Matlab and looking to find a neat way of implementing some steps.
The task
  1. gather some data from a csv file. These are an independent variable column and a series of other dependent data columns, my "curves".
  2. calculate a polynomial fit for each of the curves. Degree is fixed.
  3. calculate the intersection abscissa of each of those polynomials with a specific but otherwise programmable value
  4. calculate the value of the first derivative of each polynomial at the abscissa found in 3)
  5. plot values calculated in 4)
My implementation
  1. use importdata() to gather from csv, store result in a matrix (no problem)
  2. use polyfit() to create the polynomial. Note: I had to use a for statement here to address each column of data against the X values. Isn't there a more compact way like D=polyval(X,A,4) where both D and A are matrices and X is a column vector? I had to use something like:
for i = 1:6
P(i,:)= polyfit(A.data(:,1),A_data(:,i),4);
D(i,:)= polyder(P(i,:));
end
3. this is where it gets tricky . I started with the function roots() but could not find where to "tell" MATLAB that I was interested in a specific interval only. Then I found fzero() which allows you that. However, then I got the "problem" of defining a function from my polynomial coefficients. For this, I defined a function in a file, myfun.m, basically looking like this:
function [ y ] = myfun( x )
y=polyval([1 2 3 4],x);
end
and then using fzero(@myfun,x_0). This works syntactically, however I have an issue there. As far as I understood, to use fzero() the definition of the function must have a single input parameter, above denoted as "x". But then to use my calculated polynomials, I have to have them as variables inside the scope of the function, although I would much more like to pass them as parameters. A more complete implementation of the function looks like this:
function [ y ] = myfun( x )
global target;
A=importdata(myfile.csv,',',1);
A_data = A.data(:,2:7);
for i = 1:6
P(i,:)= polyfit(A.data(:,1),A_data(:,i),4);
D(i,:)= polyder(P(i,:));
end
y=polyval(P(6,:),x)-target;
end
where I worked around the lack of input parameters by loading the data directly in the function and attempting to use a global variable declaration for the exact cross I wanted. Still, I'd have to declare a global index for the chosen row in matrix P, something like:
y=polyval(P(index,:),x)-target;
as to be able to run a for loop where I iteratively build a vector of needed results.
I can try to experiment with another global variable for the indexing, and also declare the data and polynomials matrices as global in order not to have them repeatedly calculated when not needed. However I would like to ask you guys your take on my algorythm and I am sure there is much better ways to implement my task.
Would you mind giving some hints?
Thanks and kind regards,
Michele

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jun 2014
Michele - you could try to use an anonymous function (see http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html for details) as your myfun.
Suppose that you have done all of the following (which you had to add to your function to get around the inability pass additional parameters)
A=importdata(myfile.csv,',',1);
A_data = A.data(:,2:7);
for i = 1:6
P(i,:)= polyfit(A.data(:,1),A_data(:,i),4);
D(i,:)= polyder(P(i,:));
end
target = 12345; % I'm not sure what this is so have just assigned some number
index = 1; % again as not sure on this, it may depend on the for loop that
% you mentioned
Now define your function as
myfun = @(x)polyval(P(index,:),x)-target;
x is is the variable input argument (since enclosed in brackets immediately following the @), and P, index and target are the known values. Then you should be able to pass just this function to fzero.
If you have a loop that changes any of the known values (index in particular) then just re-assign myfun whenever these values change.
Try the above and see what happens!
  1 Comment
Michele
Michele on 18 Jun 2014
Geoff - Thanks for your prompt answer, I will look into the anonymous function construct.
The parameter target is used to set the crossing point I want to find. Since the fzero calculates...well...the zero :), I find the zero of whatever is my function - target. If you know any other method, please tell me.
After asking, I actually implemented my small script using P and D matrices as global, so that I could access them from within myfun() but otherwise create them from outside of the function.
I have briefly read about the anonymous function in the help link and it looks like it would do...I will let you know once I will have experimented with it.
Thank you!
Michele

Sign in to comment.

More Answers (1)

Michele
Michele on 18 Jun 2014
Sorry guys this is a bit of a side question:
Together with Jeoff's reply, I also got an answer from user Jangan, which no matter what I do, I cannot visualize. Every time I click the link (which has a different parameter), I end up looking at Jeoff's reply.
I then thought I should maybe "accept" his answer as to proceed further with other answers, but that's not the case.
I would really be interested in what user Jangan had to say!
Thanks for helping,
Michele
p.s. I am using IE...is that an issue?

Categories

Find more on Polynomials in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!