Error using inline (line 51) Input must be a character vector.

Error using inline (line 51) Input must be a character vector.

8 个评论

In response to the flag by @MFP Odlanra ("My question has been answered. I am a private person and I don't want my name on the internet and that's why I want to remove it.")
You should have thought about that before posting your question. You can change your user name in the Mathworks profile page. That should also change the content of the @-mention in this comment. Feel free to post a comment after changing your display name if that isn't the case.
I am a private person and I don't want my name on the internet
In situations such as that, you should hire a private consultant. It might take a bit of work to arrange anonymous payment, though.
Backup with original question asked by @MFP Odlanra is on Way Back Machine
Terms of Use for this forum:
Glad I made the backup to the Wayback Machine..
@MFP Odlanra Editing away your question after receiving an answer is extremely rude (and pointless).
I have restored the question and removed the previous name information.
If you had only been concerned about privacy, you would not have deleted the restored image.

请先登录,再进行评论。

 采纳的回答

s = input('Input the function interms of g(x)\n','s') ;
Try using 's' in input, this will convert the entered value into a string. inline needs string. When you have entered 0, it is taken as a number and you are getting error.

19 个评论

While KSSV is correct that you need the 's' option in your call to input there is another issue here.
Do not use inline. We started discouraging its use probably over a decade ago.
Use str2func to create an anonymous function instead.
for j = N1+1:N1+1
t =(j-1)*k;
fprintf('For t = %.2f\n',t)
fprintf('\n');
fprintf(' i j x t wij WIJ\n');
for i = 1:m+1
x = (i-1)*h;
fprintf('%2.0d %6.0f %6.0f %7.1f %12.3f %14.5f\n', i-1, j-1, x, t, wij(i,j),WIJ(i,j));
end
end
How do I make a 2-D or 3-D animation of this?
I tried using
ti=0:k:t;
xi=0:h:l;
figure(1)
surf(ti,xi,wij);
str = sprintf('For t = %.2f\n',t);
str1 = sprintf('SAMPLE:');
title({str1,'PROBLEM'},str)
xlabel('Time')
ylabel('Distance')
zlabel('wij')
But the figure doesn't move
for j = N1+1:N1+1
t =(j-1)*k;
ti=0:k:t; %remember t has to be set before you use t
xi=0:h:l;
calculate wij array at this point
surf(ti, ti, wij)
str = sprintf('t = %.2f', t);
title( str );
drawnow();
end
Is this for the last code or the first code that I mentioned? Sorry, I got confused.
You missed the step I posted
calculate wij array at this point
You would calculate it based upon the ti and xi vectors, which are changing each time because your ti has an upper limit of t
Also, I had a typing mistake and the surf line should have been
surf(ti, xi, wij)
But perhaps I am misunderstanding? Perhaps you have pre-calculated wij and WIJ, and your intention is to animate it with respect to time, making more and more of it visible along the time axes? Your display of all the coordinates inside the loop does not make sense in that situation, as you would be displaying the same values (but perhaps more of them) for every for j iteration.
"But perhaps I am misunderstanding? Perhaps you have pre-calculated wij and WIJ, and your intention is to animate it with respect to time, making more and more of it visible along the time axes?"
Yep. I've already calculated the wij values and only want to animate with respect to time.
ti = 0 : k : N1*k;
xi = 0 : h : l;
for j = 2:N1+1
t = (j-1)*k;
surf(ti(1:j), xi, wij(:,1:j), 'edgecolor', 'none')
str = sprintf('t = %.2f', t);
title( str );
drawnow();
end
I would not be surprised if you turn out to need
surf(ti(1:j), xi, wij(:,1:j).', 'edgecolor', 'none')
You are doing some confusing things with the coordinates.
Please remember that in arrays, x is the column and y is the row, so for example if you have 3 x values and 5 y values then you would surf(vector of 3 x, vector of 5 y, array that is 5 y by 3 x) . But in your code, your i is used for x dimension and your j is used for t dimension, so if you want your x variable to appear as the x (horizontal) coordinate, then your i should be the second index not the first. I think you might have built your wij the wrong way around taking that into account, so I suspect you will need the .' version.
Here is some tested sample code:
k = 3;
N1 = 25;
h = 1.75;
N2 = 20;
l = h*N2;
ti = 0 : k : N1*k;
xi = 0 : h : l;
[TI, XI] = ndgrid(ti, xi);
wij = cos(TI*pi) - XI.^2 + XI;
for j = 2:N1+1
t = (j-1)*h;
surf(ti(1:j), xi, wij(1:j,:).', 'edgecolor', 'none')
str = sprintf('t = %.2f', t);
title( str );
xlim(ti([1,end]))
pause(0.5)
end
the wij is an array
thus
I'm still not able to animate the wave. I was wondering if anyone could check my code.
It doesn't move in any direction at all.
wij is shown in the right side window
i can send yall the code if ur willing to check thank u
Notice in my sample I showed
surf(ti(1:j), xi, wij(1:j,:).', 'edgecolor', 'none')
^
Whereis in your error message of a couple of days ago, https://www.mathworks.com/matlabcentral/answers/741217-error-using-inline-line-51-input-must-be-a-character-vector#comment_1323842 your error message shows wij(1:j) with no second index.
The sample code I did earlier was designed to use a fixed view and fill the data progressively. However, in your latest version above you are always surf() the same full wij. Your doing that reminded me that there is another method of animation, which is to not hold the view steady, and to instead plot everything at the same time but let the view zoom out over time.
k = 3;
N1 = 25;
h = 1.75;
N2 = 20;
l = h*N2;
ti = 0 : k : N1*k;
xi = 0 : h : l;
[TI, XI] = ndgrid(ti, xi);
wij = cos(TI*pi) - sin(XI) + TI;
surf(ti, xi, wij.', 'edgecolor', 'none')
for j = 2:N1+1
t = (j-1)*h;
str = sprintf('t = %.2f', t);
title( str );
xlim(ti([1,j]))
pause(0.5)
end
Tried this, however, after I run the program it says
Error using surf (line 71)
Data dimensions must agree.
Error in
surf(ti,xi,wij.','edgecolor','none')
I cleaned it up a bit.
With those particular inputs, it is not obvious that it is animating in time, because with those inputs it is constant in time.
%% VARIABLES THAT ARE REQUIRED TO RUN THE PROGRAM
syms('x','s')
fprintf('PROVIDE THE NECESSARY INFORMATION FOR THE PROGRAM TO RUN.\n\n');
s = 110*sin((pi*x)/200);
f = matlabFunction(s, 'vars', x);
s = input('g(x) = ','s');
g = matlabFunction(str2sym(s), 'vars', x);
s = 5.5*cos((pi*x)/200);
F = matlabFunction(s, 'vars', x);
s = input('G(x) = ','s');
G = matlabFunction(str2sym(s), 'vars', x);
fprintf(1,'FOR THE OTHER INPUT VARIABLES\n');
L = 0.3;
C = 0.1;
l = 200;
T = 0.5;
m = 20;
N = 5;
alpha = sqrt(1/(L*C));
h = l/m;
fprintf('\n\n\n\n')
% STEP 1
k = T/N; % STEP-SIZE FOR THE MAXIMUM TIME
lambda = alpha*k/h;
wij = zeros(m+1, N+1);
WIJ = zeros(m+1, N+1);
% STEP 2
for j = 2:N+1 %Original >> j = 1:N
wij(1,j) = 0; %Original >> wij(0,j) = 0
wij(m+1,j) = 0; %Original >> wij(m,j) = 0
end
for J = 2:N+1 %Original >> j = 1:N
WIJ(1,J) = 0; %Original >> wij(0,j) = 0
WIJ(m+1,J) = 0; %Original >> wij(m,j) = 0
end
% STEP 3
wij(1,1) = f(0); %Original >> wij(0,0) = f(0)
wij(m+1,1) = f(l); %Original >> wij(m,0) = f(l)
WIJ(1,1) = F(0); %Original >> wij(0,0) = f(0)
WIJ(m+1,1) = F(l); %Original >> wij(m,0) = f(l)
% STEP 4 (INITIALIZE FOR t=0 AND t=k)
for i = 2:m %Original >> i = 1:m-1
t = k;
wij(i,1) = f(h*(i-1)); %Original >> wij(i,0) = f(i*h)
wij(i,2) = (1-lambda^2)*f(h*(i-1))+lambda^2*(f(i*h)+f(h*(i-2)))/2+k*g(h*(i-1)); %Original >> wij(i,1) = (1-(lambda)^2)*f(i*h)+(((lambda)^2)/2)*(f((i+1)*h+f((i-1)*h)))+k*gc(i*h)
end
for I = 2:m %Original >> i = 1:m-1
t = k;
WIJ(I,1) = F(h*(I-1));
WIJ(I,2) = (1-lambda^2)*F(h*(I-1))+lambda^2*(F(I*h)+F(h*(I-2)))/2+k*G(h*(I-1));
end
% STEP 5 (PERFORM MATRIX MULTIPLICATION)
for j = 2:N %Original >> j = 1:N
for i = 2:m %Original >> i = 1:m-1
wij(i,j+1) = 2*(1-lambda^2)*wij(i,j)+lambda^2*(wij(i+1,j)+wij(i-1,j))-wij(i,j-1); %Original >> Wij(i,j) = (1-(lambda)^2)*wij(i,j)+((lambda)^2)*(wij(i+1,j)+wij(i-1,j))-wij(i,j-1)
end
end
for J = 2:N %Original >> j = 1:N
for I = 2:m %Original >> i = 1:m-1
WIJ(I,J+1) = 2*(1-lambda^2)*WIJ(I,J)+lambda^2*(WIJ(I+1,J)+WIJ(I-1,J))-WIJ(I,J-1); %Original >> Wij(i,j) = (1-(lambda)^2)*wij(i,j)+((lambda)^2)*(wij(i+1,j)+wij(i-1,j))-wij(i,j-1)
end
end
% STEP 6
for j = 1:N+1 %Original >> j = 0:N
t =(j-1)*k; %Original >> t = j*k
fprintf('\n');
fprintf('For t = %.2f\n',t)
fprintf(' i j x t Voltage Current\n');
for i = 1:m+1 %Original >> i = 0:m
x = (i-1)*h; %Original >> x = i*h
fprintf('%2.0d %6.0f %6.0f %7.1f %12.3f %14.5f\n', i-1, j-1, x, t, wij(i,j),WIJ(i,j));
end
end
% STEP 7
fprintf('\n')
fprintf('MAXIMUM ITERATION EXCEEEDED: i = %d\n', i)
disp('"THE PROCEDURE IS COMPLETE"');
fprintf('\n\n\n');
fprintf('TOTAL NUMBER OF ITERATIONS: i = %d\n', i*N);
fprintf('\n\n\n');
% VISUALIZING THE WAVE EQUATION
ti=0:k:t;
xi=0:h:l;
[TI, XI] = ndgrid(ti, xi);
figure(1)
surf(ti,xi,wij,'edgecolor','none')
colorbar;
for j = 2:N+1
t = (j-1)*k;
str = sprintf('For t = %.2f\n',t);
str1 = sprintf('Algorithm 12.4:');
title({str1,'WAVE'},str);
xlabel('Time');
ylabel('Distance');
zlabel('Voltage');
xlim(ti([1,j]));
pause(0.5)
end
%% END OF PROGRAM

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by