필터 지우기
필터 지우기

sqrt and norm doesn't return exact value

조회 수: 2 (최근 30일)
sam
sam 2014년 11월 15일
편집: Matt 2014년 11월 15일
Hello guys I'm new to matlab and i ran into a problem where the code doesn't return the exact value in my case, i want to calculate the norm which is sqrt(a^2+b^2)
syms x y
f(x,y)=((x-1)^2+10*(y-x^2)^2)
f(-1,2)
fx=gradient(f,x)
fy=gradient(f,y)
g=[fx(-1,2) fy(-1,2)]
normg=norm(g)
normg2=((g(1)^2+g(2)^2)^0.5)
normg3=sqrt(g(1)^2+g(2)^2)
I've tried doing it manually, and with norm and sqrt function but still giving me 1696^(1/2) which is correct but i want it to give me 41.18252
any thoughts? thank you

답변 (3개)

Geoff Hayes
Geoff Hayes 2014년 11월 15일
Sam - see Perform Symbolic Compuations for some ideas on how to simplify the output of a symbolic expression. In your case, that would be
simplify(normg2)
simplify(normg3)
Try the above and see what happens!
  댓글 수: 2
sam
sam 2014년 11월 15일
it is still the same answer :(
Geoff Hayes
Geoff Hayes 2014년 11월 15일
Did you check the link on performing symbolic computations? Please post the results from each of
>> normg3
and
>> simplify(normg3)
What happens if you try
>> eval(normg3)

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


MA
MA 2014년 11월 15일
syms x y
f=@(x,y)((x-1)^2+10*(y-x^2)^2);
vpa(f(-1,2))
fx=gradient(f,x);
fx=subs(fx,x,-1);
fx=subs(fx,y,2);
fy=gradient(f,y);
fy=subs(fy,x,-1);
fy=subs(fy,y,2);
g=[fx fy]
normg=norm(g)
normg2=((g(1)^2+g(2)^2)^0.5)
normg3=sqrt(g(1)^2+g(2)^2)
  댓글 수: 1
sam
sam 2014년 11월 15일
nothing actually changed, still the same output

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


Matt
Matt 2014년 11월 15일
편집: Matt 2014년 11월 15일
Sam,
I think this does what you're asking
double(normg2)
double(normg3)
Also the eval method that Geoff suggested would work as well, but casting from symbolic to double is a less ambiguous method.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by