bisection method error symbolic

조회 수: 4 (최근 30일)
Faisal Al-Wazir
Faisal Al-Wazir 2022년 10월 7일
댓글: Faisal Al-Wazir 2022년 10월 7일
hi i'm trying to write a code to solve this question:
the code below need some adjustment to make it work
clear
clc
syms y
q=20;
g=9.8;
b=3+y;
ac=3*y+((y^2)/2);
f=@(y) 1-((q^2)/(g*ac^3)*b);
x1=0.5
x1 = 0.5000
xu=2
xu = 2
ezplot(f(y))
grid on
hold on
for i=1:100
xr(i)=(x1+xu)/2
if ((f(x1)*f(xr(i)))<0)
xu=xr(i)
elseif ((f(x1)*f(xr(i))>0))
x1=xr(i)
elseif ((f(x1)*f(xr(i))==0))
break
end
if ((i>1)&&(abs((xr(i)-xr(i-1)))/xr(i-1))*100<0.01)
break
end
plot(xr(i),f(xr(i)),'xr')
end
xr = 1.2500
Conversion to logical from sym is not possible.
xr(10)

채택된 답변

Torsten
Torsten 2022년 10월 7일
syms y
q=20;
g=9.8;
b=3+y;
ac=3*y+y^2/2;
f= 1-q^2/(g*ac^3)*b;
x1=0.5;
xu=2.5;
hold on
fplot(f,[0.5 2.5])
grid on
f = matlabFunction(f);
for i=1:100
xr(i)=(x1+xu)/2;
if ((f(x1)*f(xr(i)))<0)
xu=xr(i);
elseif ((f(x1)*f(xr(i))>0))
x1=xr(i);
elseif ((f(x1)*f(xr(i))==0))
break
end
if ((i>1)&&(abs((xr(i)-xr(i-1)))/xr(i-1))*100<0.01);
break
end
end
plot(xr,f(xr),'xr')
hold off
  댓글 수: 1
Faisal Al-Wazir
Faisal Al-Wazir 2022년 10월 7일
nice but i did it in another way
clear
clc
q=20;
g=9.8;
syms w
f=@(w) 1-((q^2)/(g*(3*w+((w^2)/2))^3)*(3+w));
f=@(y) 1-((q^2)/(g*(3*y+((y^2)/2))^3)*(3+y));
x1=0.5
x1 = 0.5000
xu=2
xu = 2
ezplot(f(w))
grid on
hold on
for i=1:10
xr(i)=(x1+xu)/2
if ((f(x1)*f(xr(i)))<0)
xu=xr(i)
elseif ((f(x1)*f(xr(i))>0))
x1=xr(i)
elseif ((f(x1)*f(xr(i))==0))
break
end
if ((i>1)&&(abs((xr(i)-xr(i-1)))/xr(i-1))*100<0.01)
break
end
plot(xr(i),f(xr(i)),'or')
end
xr = 1.2500
x1 = 1.2500
xr = 1×2
1.2500 1.6250
xu = 1.6250
xr = 1×3
1.2500 1.6250 1.4375
x1 = 1.4375
xr = 1×4
1.2500 1.6250 1.4375 1.5312
xu = 1.5313
xr = 1×5
1.2500 1.6250 1.4375 1.5312 1.4844
x1 = 1.4844
xr = 1×6
1.2500 1.6250 1.4375 1.5312 1.4844 1.5078
x1 = 1.5078
xr = 1×7
1.2500 1.6250 1.4375 1.5312 1.4844 1.5078 1.5195
xu = 1.5195
xr = 1×8
1.2500 1.6250 1.4375 1.5312 1.4844 1.5078 1.5195 1.5137
x1 = 1.5137
xr = 1×9
1.2500 1.6250 1.4375 1.5312 1.4844 1.5078 1.5195 1.5137 1.5166
xu = 1.5166
xr = 1×10
1.2500 1.6250 1.4375 1.5312 1.4844 1.5078 1.5195 1.5137 1.5166 1.5151
xu = 1.5151
xr(10)
ans = 1.5151

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by