Trying to solve a matrix with four equations and four unknowns. Should be three non zero terms.

1 view (last 30 days)
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
format long;
clear all;
clc;
% 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:
% vRHS=[1;2;3;4]
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
mRHS(:,1)=[0;0;0;0];
% Solve for the solution
% "R" is a vector to store the results, and the results are always saved as
% floating-point numbers.
R=mCoeff\mRHS;
% Direct the ouput into an external file.
% You may check and print out the results from the saved file.
fid=fopen('SLS_Output.txt','wt');
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');
for i=1:nDim
for j=1:nDim-1
fprintf(fid,'%3d%c +',mCoeff(i,j),cUnk(j));
end
fprintf(fid,'%3d%c =%4d\n',mCoeff(i,4),cUnk(4),mRHS(i,1));
end
fprintf(fid,'\n');
% Print out solution:
fprintf(fid,'Solution for [a b c d]:\n\n');
for i=1:nDim
fprintf(fid,'%c =%8.4f\n',cUnk(i),R(i));
end
function SLS()
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.

Accepted Answer

Matt J
Matt J on 3 Jul 2022
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)

More Answers (1)

Shivam Lahoti
Shivam Lahoti on 3 Jul 2022
Edited: Shivam Lahoti on 3 Jul 2022
Hey,
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.
https://in.mathworks.com/matlabcentral/answers/247819-trying-to-solve-4-equation-with-4-unknowns

Community Treasure Hunt

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

Start Hunting!