printing values into a table

1 次查看(过去 30 天)
Fabiano Da Mota
Fabiano Da Mota 2015-10-18
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.

回答(1 个)

Walter Roberson
Walter Roberson 2015-10-18
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 个评论
Fabiano Da Mota
Fabiano Da Mota 2015-10-18
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 2015-10-19
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Foundation and Custom Domains 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by