I am getting an error in the code of printSol written in Numerical engineering with MATLAB?

4 ビュー (過去 30 日間)
Dhruv Bhatnagar
Dhruv Bhatnagar 2019 年 3 月 12 日
回答済み: Geoff Hayes 2019 年 3 月 13 日
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.

回答 (1 件)

Geoff Hayes
Geoff Hayes 2019 年 3 月 13 日
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

Community Treasure Hunt

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

Start Hunting!

Translated by