필터 지우기
필터 지우기

hi , i want to build a system that can calcuate the arc length where user can insert the equation, limit a and b . But i got an error, i dont know how to solve the error.

조회 수: 3 (최근 30일)
This is my surface app :
this is the coding that I create to calculate the arc length :
this is the example when I insert the equation:
here the error :

답변 (1개)

Voss
Voss 2024년 3월 12일
편집: Voss 2024년 3월 13일
  1. Use str2sym, like the error message says.
  2. The expression entered has to be valid MATLAB syntax or it's not going to work (e.g., use "2*x" instead of "2x").
  3. Use int instead of integral.
  4. The arc length calculation should have Df^2 where you have Df now.
Here's a working example:
% suppose this is your equation editfield value:
val = 'x^2 + 2*x';
% and these are your a and b values
% (you might have to convert text to numeric, e.g., using
% str2double, depending on the style of the edit fields):
a = 1;
b = 2;
% perform arc length calculation:
syms x
f = str2sym(val);
Df = diff(f,x);
f = sqrt(Df^2+1);
len = int(f,a,b) % symbolic
len = 
len = double(len) % numeric
len = 5.1003
% then assign len to the result editfield Value
% (again, you might have to convert numeric to text,
% depending on the style of ArcLengthEditField):
app.ArcLengthEditField.Value = len; % for a numeric edit field
app.ArcLengthEditField.Value = num2str(len) % for a text edit field
  댓글 수: 5
Voss
Voss 2024년 3월 13일
편집: Voss 2024년 3월 13일
Thanks @Paul for pointing that out! I've gone ahead and edited my answer to use the correct formula.
John D'Errico
John D'Errico 2024년 3월 13일
편집: John D'Errico 2024년 3월 13일
I would point out that if the function is complex enough, then int will fail to produce a result. And the function need not be too nasty either. Just a polynomial of sufficiently high order will be too much. For example, suppose the fragment under the sqrt happens to be as simple as this random polynomial:
syms x
int(sqrt(x^4 - 2*x^3 + 2),[1,2])
ans = 
Now int will fail to yield anything useful. When it returns a form like that, this tells you that int gave up.
Yes, you could then revert to vpaintegral, which would not yield a symbolic result.
vpaintegral(sqrt(x^4 - 2*x^3 + 2),[1,2])
ans = 
0.807613
And maybe that is adequate. But it would also force you to check to see if int actually did anything. Or, you could just always use vpaintegral, or even integral, after turning the function into a function handle using matlabFunction.

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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by