how do i solve this coding problem?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
Nick Counts
le 5 Nov 2016
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.
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!
0 commentaires
Voir également
Catégories
En savoir plus sur Assumptions dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!