Trying to solve a matrix with four equations and four unknowns. Should be three non zero terms.
1 ビュー (過去 30 日間)
I am trying to solve a matrix with four equations and four unknowns. I wrote the following code based off an example from my professor but I am running into an error:
>> function SLS()
% clear memory and screen
% Define parameters
nDim= 4; % Dimension of the System
% You may change the dimension for your purpose here
mCoeff = zeros(nDim,nDim); % Initialize the coefficient matrix
mRHS = zeros(nDim,1); % Initialize the righ-hand-side vector
% 1) In this example, this vector is generated as a
% 4*1 matrix
% 2) An alternative way is just to assign the values
% to an vertical array directly. For example:
cUnk = ['a' 'b' 'c' 'd']; % Array of unknown variables for output only
% Specify the values for the coefficient matrix
% In this example, the values are assgned row-by-row:
mCoeff(1,:)= [cos(2n-1)*((3*pi)/4) 0 cos(2n+1)*((3*pi)/4) 0];
mCoeff(2,:)= [(2n-1)*sin(2n-1)*((3*pi)/4) 0 (2n+1)*sin(2n+1)*((3*pi)/4) 0];
mCoeff(3,:)= [0 sin(2n-1)*((3*pi)/4) 0 sin(2n+1)*((3*pi)/4)];
mCoeff(4,:)= [0 (2n-1)*cos(2n-1)*((3*pi)/4) 0 (2n+1)*cos(2n+1)*((3*pi)/4)];
% Specify the values for RHS vector
% Solve for the solution
% "R" is a vector to store the results, and the results are always saved as
% floating-point numbers.
% Direct the ouput into an external file.
% You may check and print out the results from the saved file.
fprintf(fid,'Mech012 Solving for Linear Equation System\nExample Code\nBy Xiao Liu(xil307)\n\n');
% Print out linear equations:
fprintf(fid,'Linear Equation System:\n\n');
% Print out solution:
fprintf(fid,'Solution for [a b c d]:\n\n');
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
Matt J 2022 年 7 月 3 日
You cannot paste a function definition into the command window. Put it in a file and run it in the Maltab editor. Also, you are missing lots of multiplication operators in your code, e.g., cos(2n-1) should be cos(2*n-1)
その他の回答 (1 件)
Shivam Lahoti 2022 年 7 月 3 日
編集済み: Shivam Lahoti 2022 年 7 月 3 日
Look into your code closely you are missing lot of operators, that must be the reason it throws error. you might want to calculate the values in a different way if it still not works then you shall refer to the following answer and solve for all the four variables.