필터 지우기
필터 지우기

how do i solve this coding problem?

조회 수: 1 (최근 30일)
GISEOK LEE
GISEOK LEE 2016년 10월 27일
답변: Nick Counts 2016년 11월 5일
clear all, close, clc,
syms f(x),
syms a,
syms b;
f(x) = x-2*sin(x);
x1=1.0; x2=2.0; iternum = 0;
if(f(x1)*f(x2)<0)
x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
else
printf('false'\n);
end;
while(1)
A = [x1^2 x1 1; x2^2 x2 1; x^3 x3 1];
B = [f(x1); f(x2); f(x3)];
X_co = inv(A)*B;
a = X_co(1,1); b = X_co(2,1); c = X_co(3,1);
X = [-b+sqrt(b^2-4*a*c)/(2*a); (-b-sqrt(b^2-4*a*c))/(2*a)];
x4 = X(1,1); x5 = X(2,1);
iternum = iternum +1;
if((x4>x1) && (x4 < x2))
x1 = x4;
else
x1 = x5;
end;
if(abs(f(x1))<0.01)
break;
end;
end;
the resualt is conversion to logical from sym is not possible

답변 (1개)

Nick Counts
Nick Counts 2016년 11월 5일
when you write an expression like (x4 < x1) when both x4 and x1 are sym objects, Matlab builds a new symbolic expression. It does not evaluate that expression.
It's just like you wrote "2sin(t) < 1"
Is that true or false?
There's no way to answer that without more information.
To evaluate a symbolic expression you need to use something like solve
Also look at your while loop. You are changing iternum each iteration, but that value is never used, so each time through the loop the conditions are identical. If the loop didn't break out the first time, it would never break out!
Definitely check out the documentation for sym / syms. You could also run these checks by stepping through a bunch of x values, which is kind of brute force, but sometimes brute force is the fastest/easiest approach
Good luck!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by