syms to another .m file

조회 수: 1 (최근 30일)
Kaylie
Kaylie 2025년 2월 22일
답변: Walter Roberson 2025년 2월 22일
How do you export your syms equation to a new/different .m file to be able to use the equation with parameters.

채택된 답변

Walter Roberson
Walter Roberson 2025년 2월 22일
As @Torsten says, you can just pass symbolic expressions as parameters.
However, you need to be careful about assumptions. All levels of MATLAB code share the same assumptions, and using syms on a variable resets assumptions.
syms x positive
assumptions
ans = 
f = sin(x)+x^2
f = 
testit(f, x)
ans = 
ans = Empty sym: 1-by-0
ans = 
ans = 
assumptions
ans = Empty sym: 1-by-0
function testit(f, var)
assumptions
syms x
assumptions
diff(f, x)
diff(f, var)
end

추가 답변 (1개)

Torsten
Torsten 2025년 2월 22일
편집: Torsten 2025년 2월 22일
Call the other .m-file with the syms-equation (or function) as input argument.
syms x
f = x.^2;
result = fun(f)
result = 
4
function result = fun(f)
syms x
result = subs(f,x,2);
end

카테고리

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

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by