Importing data at a regular interval for analysis

7 views (last 30 days)
Alex on 30 Jan 2012
Say I have an array in a .csv file like this:
and I want to solve this set of equations for g, h, and k:
If I wanted to solve this set of equations using the values in the first three rows of data, and then solve the same three equations using the values in the second three rows and continue the evaluation for n rows of data, how would I set that up? I was thinking of defining each term in the equation as a csvread (b=csvread(filename, row, column)), but I don't know hot to handle the reading of different rows by a regular increase (3 rows at a time through the whole dataset). Any ideas?

Accepted Answer

Bård Skaflestad
Bård Skaflestad on 30 Jan 2012
What are you actually trying to accomplish? Is it to solve a sequence of systems of three (highly) non-linear equations defined more or less by
G(X, Y1) == 0
G(X, Y2) == 0
G(X, Y3) == 0
in which X=[g,h,k], Y=[a,b,c,d,e,f], and
f1 = @(x, y) x ./ y;
f2 = @(x, y, z) (x - y) .* z;
f3 = @(x, y, z, w) f1(x, y).^z + w
f4 = @(b, c, d, e, f, g, h, k) ...
f3(f2(b, c, g), f2(d, e, h), f, k) - f
G(X,Y) = f4(Y(2), Y(3), Y(4), Y(5), Y(6), ...
X(1), X(2), X(3))
If so, you need to devise a method for solving a single such system given a particular set of parameters a..k . Then, solving a sequence of such systems is a trivial loop (typically stride three) over the rows of the data matrix input using csvread.
If the above is not what you're after, then you will need to be a lot more precise concerning what you want to accomplish. And do, please, include the details of what you've done so far.
  1 Comment
Alex on 31 Jan 2012
You are correct, the above is what I wanted to do. The actual equation is for calculating Reflectance. It is:
where e is 2.718... and all values are known except n1, k1, and d1.
I wanted to use the fsolve command to solve 3 of these at a time (the data is all in the table) but the table has 850 equations worth of values that need to be used.

Sign in to comment.

More Answers (1)

Bård Skaflestad
Bård Skaflestad on 1 Feb 2012
I'm adding another answer based on your comment to my previous answer. I wanted to simply add another comment, but I need code markup (and the most up-voted answer to question 994 also points out the lack of markup capabilities in comments...)
Anyway, this is a perfect case for closures (effectively, parametrised function handles). I'd write a simple generator function like
function F = generate_system(p)
assert (ndims(p) == 2, ...
'System parameters ''P'' must be 2D matrix.');
assert (all(size(p) == [3, 5]), ...
'System parameters must be 3-by-5 matrix.');
b = p(:,1);
c = p(:,2);
d = p(:,3);
e = p(:,4);
f = p(:,5);
q = (b - c) ./ (d - e);
F = @(x) (q .* x(1) / x(2)).^f + x(3) - f;
which, when called with parameter matrix p consisting of three consecutive rows of the parameter data from your .csv file, will return a function handle that defines a particular system of non-linear equations. This handle can be passed on to fsolve along with an initial guess to affect the numerical solution of this particular system.
Given generate_system above, you can solve the sequence of non-linear systems using a script like the following
data = csvread('data.csv');
assert (mod(size(data, 1), 3) == 0, ...
'Input data must have an integer multiple of 3 rows');
assert (size(data, 2) == 6, ...
'Input data must have exactly six columns.');
nsys = size(data, 1) / 3;
soln = zeros([nsys, 3]);
for k = 1 : nsys,
F = generate_system(data(3*k + (1:3), 2:end));
X0 = some_initial_guess;
soln(k, :) = fsolve(F, X0);
at least if all of the individual systems have a solution.
I'm afraid I don't have the Optimization Toolbox installed, so I cannot actually test this thing. Hopefully, it will at least provide some hints as to how to proceed.
Bård Skaflestad
Bård Skaflestad on 1 Feb 2012
No problem. Of course I did make an error in the parameter subset extraction. It should be
F = generate_system(data(3*(k-1) + (1:3), 2:end))
Terribly sorry about that.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!