Take data in, fit them to poly, calculate derivative at arbitrary cross points
6 views (last 30 days)
Show older comments
Hi All,
first post here. I am new to Matlab and looking to find a neat way of implementing some steps.
The task
- gather some data from a csv file. These are an independent variable column and a series of other dependent data columns, my "curves".
- calculate a polynomial fit for each of the curves. Degree is fixed.
- calculate the intersection abscissa of each of those polynomials with a specific but otherwise programmable value
- calculate the value of the first derivative of each polynomial at the abscissa found in 3)
- plot values calculated in 4)
My implementation
- use importdata() to gather from csv, store result in a matrix (no problem)
- 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
0 Comments
Accepted Answer
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!
More Answers (1)
Michele
on 18 Jun 2014
1 Comment
Geoff Hayes
on 18 Jun 2014
I can't see his response either. It may have been a spam answer that has since been removed.
See Also
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!