I am getting an error in the code of printSol written in Numerical engineering with MATLAB?
4 views (last 30 days)
Show older comments
function printSol(xSol,ySol,freq)
% Prints xSol and ySoln arrays in tabular format.
% USAGE: printSol(xSol,ySol,freq)
% freq = printout frequency (prints every freq-th
% line of xSol and ySol).
[m,n] = size(ySol);
if freq == 0;freq = m; end
head = ’
for i = 1:nx’;
head = strcat(head,’ y’,num2str(i));
end
fprintf(head); fprintf(’\n’)
for i = 1:freq:m
fprintf(’%14.4e’,xSol(i),ySol(i,:)); fprintf(’\n’)
end
if i ˜= m;
fprintf(’%14.4e’,xSol(m),ySol(m,:));
end
Bold letter showing error in the matlab command window ''Error: File: printSol.m Line: 19 Column: 6
The expression to the left of the equals sign is not a valid target for an assignment.''
What is the problem.
0 Comments
Answers (1)
Geoff Hayes
on 13 Mar 2019
Dhruv - I wonder if when you copied the above code from Numerical Methods in Engineering with MATLAB that you didn't introduce some "special" characters that don't work well with MATLAB. For example, in your above code, the single quotes are invalid, as is the tilde ~, the assignment for head is incomplete, etc.. Try using the following code instead
function printSol(xSol,ySol,freq)
% Prints xSol and ySoln arrays in tabular format.
% USAGE: printSol(xSol,ySol,freq)
% freq = printout frequency (prints every freq-th
% line of xSol and ySol).
[m,n] = size(ySol);
if freq == 0;freq = m; end
head = ' x' ;
for i = 1:n
head = strcat(head,' y',num2str(i));
end
fprintf(head); fprintf('\n');
for i = 1:freq:m
fprintf('%14.4e',xSol(i),ySol(i,:)); fprintf('\n');
end
if i ~= m;
fprintf('%14.4e',xSol(m),ySol(m,:));
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!