printing values into a table

조회 수: 1 (최근 30일)
Fabiano Da Mota
Fabiano Da Mota 2015년 10월 18일
댓글: Walter Roberson 2015년 10월 19일
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