MATLAB Answers

Help iterating

1 view (last 30 days)
Alex on 7 Mar 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
Hey all,
I've been working with a program that uses data from a spreadsheet, and solves an equation for each row of data. I was using the fsolve command, but to check that the equation we were using was correct, we decided to solve the equation for a single variable with all others known, so I need to re-tool the program to solve a simple single variable iteration, but I'm having some difficulties. The code and function file are below:
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) == 7, ...
'Input data must have exactly seven columns.');
nsys = size(data, 1) / 2;
soln = zeros(nsys, 2);
for k = 1 : nsys,
F = back_check_solve_gen(data(2*(k-1) + (1:2), 1:end));
soln(k,:) = F;
end
fid = fopen('results_back_check.csv','w');
fprintf(fid, '%5.5f\n', soln);
fclose(fid);
Function file:
function F = back_check_gen(p)
assert(ndims(p) == 2, ...
'System parameters ''p'' must be 2D matrix.');
assert(all(size(p) == [2,7]), ...
'System parameters must be 2-by-7 matrix.');
y = p(:,1);
n0 = p(:,2);
n2 = p(:,3);
n3 = p(:,4);
k2 = p(:,5);
k3 = p(:,6);
n1 = 2.6;
k1 = 1.3;
d1 = .34;
d2 = 300;
F = ...
end
Where F is an equation that is too long to define here (and an equation that doesn't need to be checked, that is the purpose of this exercise). When I run the program, the resulting graph from the data points generated has the appropriate shape, but the shape is not in the correct scale. It looks compressed. When I run the program with soln(k,:) = F unsupressed, I noticed that it was solving for soln many times, just just for many iterations, such that:
soln = some 2x425 array
soln = some different 2x425 array
soln = some even more different 2x425 array
etc...
So, my question is, why? And how do I get a single array for soln?
Thanks.
0 CommentsShowHide -1 older comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!