2017-03-23 5 views
2

그래서 Matlab에는 JavaScript 코드로 복사하여 붙여 넣기를 사용하여 숫자 솔루션으로 애니메이션을 적용하고 싶습니다. 문제는 내 코드의 일부 장소가 지수 (대개^2)를 얻게되어서 Matlab에 A * A로 표현되도록 할 수 있다는 것입니다.Express를 Matlab의 A * A로 표현하십시오.

나는 차라리

cos(th2t)*cos(th2t) 

나는이 작업을 수행 할 수있는 모든 방법을

cos(th2t)^2 

같은 의해 복수의 (다른) 표현 표명 한 것 가지고? 다른 방법은 이후에 텍스트 편집기를 사용하여 2의 제곱을 검색하고이를 대체하는 것입니다. 그러나 여러 다른 표현식이 있으므로 시간이 좀 걸릴 것입니다 ...

이것은 제가 끝내는 표현 중 하나의 예입니다 최대 :

(J31*(2*ddth2t*cos(th1t) - 2*w12*w13 - 4*dth1t*dth2t*sin(th1t) - 4*dth2t*w13*sin(th1t) + dth1t^2*sin(2*th2t)*cos(th1t) - w12^2*sin(2*th2t)*cos(th1t) + w13^2*sin(2*th2t)*cos(th1t) + 2*dth2t*w11*sin(2*th2t) + 2*w12*w13*cos(th2t)^2 + ddth1t*sin(2*th2t)*sin(th1t) + 4*dth1t*dth2t*cos(th2t)^2*sin(th1t) + 2*dth1t*w13*sin(2*th2t)*cos(th1t) + 4*dth2t*w13*cos(th2t)^2*sin(th1t) + w11*w12*sin(2*th2t)*sin(th1t) + 4*dth1t*w12*cos(th1t)^2*cos(th2t)^2 - 2*dth1t*w11*sin(2*th1t)*cos(th2t)^2 - 2*dth2t*w11*sin(2*th2t)*cos(th1t)^2 + 2*w12*w13*cos(th1t)^2*cos(th2t)^2 - w11*w13*sin(2*th1t)*cos(th2t)^2 - 4*dth2t*w12*cos(th1t)*cos(th2t)*sin(th1t)*sin(th2t)))/(2*(J11 + J31)) 
+1

''^'을 (를) 검색하기 위해 ['regexp'] (http://mathworks.com/help/matlab/ref/regexp.html)를 작성하고 그 앞에있는 식으로 여러 번 사용하십시오. (나는 정규 표현할 수 없기 때문에 거기에서 당신을 도울 수 없다). – Adriaan

+0

@Adriaan이 작동 할 수도 있습니다. 나는 정규 표현식을 가지고 있지 않기 때문에, 변경 내용을 하드 코딩하는 것이 텍스트 편집기일지도 모른다. ... –

+0

'syms th2t;''factor (cos (th2t)^2)' –

답변

1

** 연산자에 의존하려면 Steve의 대답이 잘 작동해야합니다. 그러나 해당 연산자가 공식적으로 지원되지 않으며 해당 솔루션이 OP 질문에 직접 응답하지 않기 때문에 여기에 지수 식으로 지수를 확장 할 수있는 함수가 있습니다.

function [ text ] = remove_powers(exp) 
%Cleans the powers from T, and return expanded text representation 

    % Define functions 
    enclose [email protected](t) ['(' t ')']; 
    [email protected](b,p) strjoin(strcat(cell(1,p),enclose(char(b))),'*'); 
    [email protected](s) arrayfun(@(k) count(char(s(k)),'^'), 1:length(s)); 
    sym2str = @(s) strrep(char(s), ' ', ''); 

    % Check for fractions 
    [N,D]=numden(exp); 
    if ~isequal(D,sym(1)) 
     % pass up the num and den 
     text = [remove_powers(N) '/' enclose(remove_powers(D))]; 
    else 
     % Split a into subterms 
     Ts = children(exp); 

     % Clean children 
     has_pow=count_pow(Ts)>0; 
     text = sym2str(exp); 
     if sum(count_pow(Ts))<count_pow(exp) 
      % We have removed a power, clean it, expand it, and pass up 
      text = expand_pow(remove_powers(Ts(1)),Ts(2)); 
     else 
      % Just clean the unclean children and pass up 
      for t=Ts(has_pow) 
       text = strrep(text,sym2str(t),remove_powers(t)); 
      end 
     end 
    end 
end 

함수 재귀 부모 각 서브 표현식을 청소 (텍스트)를 대체 매트랩 children 함수를 사용한다. 스티브 (Steve)가 cos(x^2+2*y)^2에 대한 주석에서 언급했듯이이 방법은 구문 분석 문제를 피하기 때문에 regex를 사용하는 것보다 낫습니다.

syms x y real 
exp = cos(x^2+2*y)^2; 
cleaned_exp = remove_powers(exp) 

출력 : 좋은 예있게

(cos(2*y+(x)*(x)))*(cos(2*y+(x)*(x)))

매트랩 구문 분석을하고 있기 때문에, '^'사업자에 대한 우선 순위를 분석 할 필요가 없었다 공지 것을 , 정규 표현식으로 달성하기가 어려울 수 있습니다.

예상대로
syms ddth1t dth1t th1t ddth2t dth2t th2t w11 w12 w13 J11 J31 real 
exp = (J31*(2*ddth2t*cos(th1t) - 2*w12*w13 - 4*dth1t*dth2t*sin(th1t) - 4*dth2t*w13*sin(th1t) + dth1t^2*sin(2*th2t)*cos(th1t) - w12^2*sin(2*th2t)*cos(th1t) + w13^2*sin(2*th2t)*cos(th1t) + 2*dth2t*w11*sin(2*th2t) + 2*w12*w13*cos(th2t)^2 + ddth1t*sin(2*th2t)*sin(th1t) + 4*dth1t*dth2t*cos(th2t)^2*sin(th1t) + 2*dth1t*w13*sin(2*th2t)*cos(th1t) + 4*dth2t*w13*cos(th2t)^2*sin(th1t) + w11*w12*sin(2*th2t)*sin(th1t) + 4*dth1t*w12*cos(th1t)^2*cos(th2t)^2 - 2*dth1t*w11*sin(2*th1t)*cos(th2t)^2 - 2*dth2t*w11*sin(2*th2t)*cos(th1t)^2 + 2*w12*w13*cos(th1t)^2*cos(th2t)^2 - w11*w13*sin(2*th1t)*cos(th2t)^2 - 4*dth2t*w12*cos(th1t)*cos(th2t)*sin(th1t)*sin(th2t)))/(2*(J11 + J31)); 

cleaned_exp = remove_powers(exp); 
isequal(sym(cleaned_exp),exp) % This should be 1 
count(cleaned_exp,'^')   % This should be 0 

, 새로운 표현이 원본과 동일하지만 더 '^'문자가 없습니다 :

은 영업 이익의 예제를 테스트합니다.

+0

이것은 정말 인상적입니다! 이 모든 작업을 해 주셔서 감사합니다. 나는 이것이 나보다 다른 누군가를 도울 수 있기를 바란다. –

1

This answer 당신이 예와 자바 스크립트에서 지수 연산자를 호출 할 수 있음을 시사한다 A**2. 따라서 ^의 모든 인스턴스를 **으로 바꿀 수 있습니다. (해답)

+0

정규 표현식이 가장 좋은 방법이라고 생각합니다. 그러나 이것은 저에게 잘 맞았습니다. –

+0

@ AndreasEvjenth이 간단한 솔루션은 ['strrep'] (http://mathworks.com/help/matlab/ref/strrep.html)로 구현할 수 있습니다. 그렇다면 적어도 자동화 될 수 있으며 Steve는 주석에서 지적했듯이 분수 구성 요소에 대해 걱정할 필요가 없습니다. 나는. 'NewFunc = strrep (OldFunc, '^', '**'); ' – Adriaan

관련 문제