Running an Iteration for single variable.
1 view (last 30 days)
Show older comments
Hey all, I've been using a closure loop with fsolve to crunch data. The below code took 3 rows of data from a .csv, and plugged it into 3 equations for simultaneous solution. I would like to modify the code to solve a single equations with a single variable, a function that fsolve is not required for. I still need to iterate, because I have to solve this equation many times sequentially, and they still need to output to a .csv. How would you go about it?
data = csvread('data.csv');
assert (mod(size(data, 1), 2) == 0, ...
'Input data must have an integer multiple of 2 rows');
assert (size(data, 2) == 6, ...
'Input data must have exactly six columns.');
nsys = size(data, 1) / 2;
soln = zeros(nsys, 2);
options=optimset('MaxFunEvals',1e10,'MaxIter',25000);
for k = 1 : nsys,
F = generate_system(data(2*(k-1) + (1:2), 1:end));
guess = [3 3];
soln(k, :) = fsolve(F, guess,options);
end
fid=fopen('results.csv','w');
fprintf(fid,'%5.5f %5.5f\n',soln);
fclose(fid);
generate_system looks like this:
function F = generate_system(p)
assert (ndims(p) ==2, ...
'System parameters ''p'' must be 2D matrix.');
assert (all(size(p) ==[2,6]), ...
'System parameters must be 2-by-6 matrix.');
y = p(:,1);
n = p(:,2);
k = p(:,3);
n0 = p(:,4);
k0 = p(:,5);
R = p(:,6);
F = @(x) ((((n0-(n-1i.*k))./(n0+(n-1i.*k)))+(((n-1i.*k)-(x(1)-1i.*x(2)))./((n-1i.*k)+(x(1)-1i.*x(2)))).*(exp(-2.*1i.*(2.*pi./y).*(n-1i.*k).*300)))./(1+((n0-(n-1i.*k))./(n0+(n-1i.*k))).*(((n-1i.*k)-(x(1)-1i.*x(2)))./((n-1i.*k)+(x(1)-1i.*x(2)))).*(exp(-2.*1i.*(2.*pi./y).*(n-1i.*k).*300))))-R;
end
Thoughts?
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!