For Loop involving n-substitution and creating it as a row vector

조회 수: 1 (최근 30일)
Hi,
I have this problem:
Edit : Sorry there is a big typo, if you spot my another typo please tell me.
syms x
n=input('the number of column for row vector= ')
for i=1:n
b(i)=input(['b',num2str(i),'= '])
end
disp(b);
y=input('input your function of variable x only= ');
for j=1:n
A=subs(y,x,j)
end
disp('your new matrix is= ')
disp(A)
%b is a row vector, shortly if input n=3
%my for loop will do 3 input and created it as a row vector
% That is matrix of 1×3 (a row vector)
For example if i input
n=3
b1=1
b2=2
b3=3
y=x^2
Hopefully, it will display something like
your new matrix is =
1 4 9
I'm squaring for each b.
But when i'm trying this, shamefully this only shows 9 only. 1 and 4 are missing.
very kind of you if you want to help me.
Thanks in advance.

채택된 답변

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 7일
n=input('the number of column for row vector= ');
for i=1:n
b(i)=input(['b',num2str(i),'= ']);
end
disp(b);
y=input('input your function of variable x only= ','s');
y=str2func(vectorize(['@(x) ',y]));
A=y(b);
disp('your new matrix is= ')
disp(A)
  댓글 수: 2
Fahmy Shandy
Fahmy Shandy 2019년 12월 7일
편집: Fahmy Shandy 2019년 12월 7일
If i input y=x^2 (with or without little dot)
It appears
??? Undefined command/function '@(x) x.^2'.
Error in ==> my file at xx
A=y(b);
Fahmy Shandy
Fahmy Shandy 2019년 12월 7일
Thanks, for your hints and answer. I found where is my mistake.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by