printing values into a table

1 Ansicht (letzte 30 Tage)
Fabiano Da Mota
Fabiano Da Mota am 18 Okt. 2015
Kommentiert: Walter Roberson am 19 Okt. 2015
Hello Folks,
I am trying to create a code that I will solve a system of nonlinear equations and print the solutions into a table in a .doc file. The equations are functions of Theta. I want to print the solution to the system of equations for theta = 0:30:360.
I created the following code:
function F = Solution(x)
Lab = .1;
Lbc = .6;
Lcd = .3;
Lad = .4;
fid = fopen('Solution.doc','w');
fprintf(fid,'theta x1 x2 x3 x4 \n');
fprintf(fid,'_______________________________________________________\n')
n=0;
for theta=0:30:360
n=n+1;
rabx = Lab*cosd(theta)
raby = Lab*sind(theta)
F = [
(x(1))^2 + (x(2)^2)- Lbc^2;
(x(3))^2 + (x(4))^2 - Lcd^2;
raby + x(2) + x(4);
rabx + x(1) + x(3) - Lad];
tab=[theta;x(1);x(2);x(3);x(4)];
fprintf(fid,'%5d %9.f %8.3f %8.3f %12.4f %12.4f\n', tab);
end
end
When I try to run the code, using fsolve, I get the following error : Too many open files. Close files to prevent MATLAB instability.
Caused by: Unknown exception
How can I fix this?
Thanks in advance.

Antworten (1)

Walter Roberson
Walter Roberson am 18 Okt. 2015
You have
fid = fopen('Solution.doc','w');
but no-where in your code do you have
fclose(fid)
so you are leaving the file open :(
  2 Kommentare
Fabiano Da Mota
Fabiano Da Mota am 18 Okt. 2015
Hello Walter, Thank you for your response. fclose(fid) got rid of the error. Now, the Matlab is not solving the functions for each value of theta. It is only solving for one value but printing it 13 times. Please see the attached picture.
I also update the code to the following:
function F = Solution(x)
Lab = .1;
Lbc = .6;
Lcd = .3;
Lad = .4;
fid = fopen('Solution.doc','w');
fprintf(fid,'theta x1 x2 x3 x4 \n');
fprintf(fid,'_______________________________________________________\n')
n=0;
for theta=0:30:360
n=n+1;
theta
rabx = Lab*cosd(theta);
raby = Lab*sind(theta);
F = [
(x(1))^2 + (x(2)^2)- Lbc^2;
(x(3))^2 + (x(4))^2 - Lcd^2;
raby + x(2) + x(4);
rabx + x(1) + x(3) - Lad];
tab=[theta;x(1);x(2);x(3);x(4)];
fprintf(fid,'%9.3f %9.3f %8.3f %8.3f %12.4f \n', tab);
end
fclose(fid)
end
Do you have an idea of how I could fix this?
Thank you once again.
Walter Roberson
Walter Roberson am 19 Okt. 2015
The code is doing what you asked it to do, which is to print out each theta value together with the unmodified x(1), x(2), x(3), x(4) values.
Perhaps you wanted to print out the F values instead of the x values?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Foundation and Custom Domains finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by