Taking expression input and plotting it within an interval using app designer
조회 수: 2 (최근 30일)
이전 댓글 표시
I need to take a single variable expression as input from the user and give the plot for the same. I am able to take the expression input and make it a function using inline, but when I go for plotting it within an interval it shows the following error:
Error using inlineeval (line 14)
Error in inline expression ==> x^2 - 11*x + 30
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
My code for input was:
s = app.EnterHereEditField.Value;
f = inline(s);
and for plotting:
x = -10:.1:10;
y = f(x);
plot(app.UIAxes,x,y);
I am new to matlab app designer, if there's some syntax error please tell. Thanks!
댓글 수: 0
채택된 답변
Shanmukha Voggu
2021년 9월 1일
편집: Shanmukha Voggu
2021년 9월 2일
Hi Aditya,
The error(Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.) is regarding the expression(x^2 - 11*x + 30)
To understand the difference between x.^2 and x^2, let's create a square matrix x
x=[1,2;3,4]
ElementWiseSquare=x.^2 % Every element in the matrix is raised to power "2"
MatrixMultiplication=x^2 % For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix
let's create a row matrix x
x=[1,2]
ElementWiseSquare=x.^2 % Every element in the matrix is raised to power "2"
MatrixMultiplication=x^2 % This statement gives an error because columns in [1,2](first matrix) not equal to rows in [1,2](second matrix)
There can be two fixes for the issue:
1) replace the x^2 in the expression(x^2 - 11*x + 30) with x.^2
2) or make sure x is a square matrix.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!