I am creating a user define function that calculates a polynomial using the bisection method. I am getting parse error near line 14 of the file.
7 views (last 30 days)
Show older comments
function [fBis, nBis] = practice1 (y, fa, fb, eps);
nBis = 0;
x = 0;
While (x < eps)
x1 = y(fa);
x2 = y(fb);
fmid = (fa+fb)/2;
xmid = y(fmid);
x = abs((fmid-fa)/fmid);
If (x1 * xmid < 0)
fa = fa;
fb = fmid;
else
fa = fmid;
fb = fb;
end
nBis = nBis + 1;
end
fBis = fmid;
end
10 Comments
Kevin Chng
on 11 Oct 2018
Edited: Kevin Chng
on 11 Oct 2018
Bit weird on this. Do you mind copy my code in the answer, and paste it in the new script, and try to run it.
See what is the result. Put
clear all; clc;
at the beginning of the second script.
Answers (1)
Kevin Chng
on 11 Oct 2018
Edited: Kevin Chng
on 11 Oct 2018
Hi Jamilah Carlos,
y=@(f,rough,d, Re) 1./sqrt(f) + 2.0*log10((rough/d)/3.7 + 2.51./(Re.*sqrt(f)));
There are many unknown variables in your equation - rough, d, Re.
In your another script, there are rough and d variable. However, I did't see the Re.
To solve your question, you must assign those variables to your equation. Refer to my code,
function [fBis, nBis] = practice1 (y, fa, fb, eps, d, rough, Re)
nBis = 0;
x = 0;
while (x < eps)
x1 = y(fa, rough, d, Re);
x2 = y(fb, rough, d, Re);
fmid = (fa+fb)/2;
xmid = y(fmid, rough, d, Re);
x = abs((fmid-fa)/fmid);
if (x1 * xmid < 0)
fa = fa;
fb = fmid;
else
fa = fmid;
fb = fb;
end
nBis = nBis + 1;
end
fBis = fmid;
end
In your second script,
y=@(f,rough,d, Re) 1./sqrt(f) + 2.0*log10((rough/d)/3.7 + 2.51./(Re.*sqrt(f)));
dens = 999; visc = 1.12e-3; d = 0.25; v = 10; rough = 0.05e-3;
fa = 0.008; fb = 0.1; eps = 0.0001; Re= 0.2;
[fBis, nBis] = practice1 (y, fa, fb, eps, d, rough, Re)
You have to change the value of Re, i simply assign 0.2 to it.
Please accept my answer if it is working for your case.
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!