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.
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
madhan ravi
on 11 Oct 2018
y, fa, fb, eps values?
Jamilah Carlos
on 11 Oct 2018
Edited: Jamilah Carlos
on 11 Oct 2018
Jamilah Carlos
on 11 Oct 2018
Edited: Jamilah Carlos
on 11 Oct 2018
Kevin Chng
on 11 Oct 2018
Do you mind post the error here? So we could understand it better.
madhan ravi
on 11 Oct 2018
Instead of giving your codes bit by bit why not post the whole code ? It could save a lot of time!!
Jamilah Carlos
on 11 Oct 2018
Kevin Chng
on 11 Oct 2018
Edited: Kevin Chng
on 11 Oct 2018
I guess some data files or m.files are not called properly.
Try the way mentioned by Image Analyst in the link below. https://www.mathworks.com/matlabcentral/answers/140438-what-does-parse-error-basically-mean
I'm not sure what is your line16 of the undermentioned m.file.
Jamilah Carlos
on 11 Oct 2018
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.
madhan ravi
on 11 Oct 2018
Edited: madhan ravi
on 11 Oct 2018
Try elseif (x1 * xmid > 0)
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.
1 Comment
madhan ravi
on 11 Oct 2018
+1 @ Kevin nice.
Categories
Find more on Rubik's Cube 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!