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.

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

They are given in a separate m-file.
y=@(f) 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;
I forgot to add in that Re was already in the other file:
Re = dens*v*d/visc
It was included in the separate m-file with all the variables. My only problem is that there is a parse error whenever I try to end my while loop. It says syntax error. I'm not sure where it is coming from.
Do you mind post the error here? So we could understand it better.
Instead of giving your codes bit by bit why not post the whole code ? It could save a lot of time!!
I did not know it was necessary to post the other m-file since I was just creating a user-defined function that was going to be called on. It is a meant to be a general function that could solve any polynomial.
The error just says this:
parse error near line 16 of file /Users/Jami/Documents/School Work/Course Documents/Fall 2018 /ME 232A CAP Engineering Computations /Assignments/Files/practice1.m
syntax error
>>> else
I guess some data files or m.files are not called properly.
I'm not sure what is your line16 of the undermentioned m.file.
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.

Sign in to comment.

Answers (1)

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.

Categories

Find more on Rubik's Cube in Help Center and File Exchange

Asked:

on 11 Oct 2018

Edited:

on 11 Oct 2018

Community Treasure Hunt

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

Start Hunting!