solution of an implicit equation with fzero on MATLAB - implicit

I've been trying to solve this implicit equation by using fzero in MATLAB. File that holds the code is named as "colebrook" and I've typed so far is as below.
D = input('Please enter the pipe diameter in meters: ');
V = input('Please enter the fluid velocity in m/s: ');
rho = input('Please enter fluid density in kg/m^3: ');
mew = input('Please enter fluid viscosity in kg/m*s: ');
Re = D*V*rho/mew;
eps = input('Enter absolute roughness in milimeters: ');
eD = eps/(D*1000);
a = fzero(colebrookfunc,0.1);
fprintf(a);
Equation that I want to solve is kept in another m-file called "colebrookfunc", and the code it contains is as below.
function F = colebrookfunc(x)
F = x - 1./(-4 * log10(eD/3.7 + 1.256./(Re*x.^0.5))).^2;
When i run in i get this error(s):
??? Input argument "x" is undefined.
Error in ==> colebrookfunc at 2
F = x - 1./(-4 * log10(eD/3.7 + 1.256./(Re*x.^0.5))).^2;
Error in ==> colebrook at 28
a = fzero(colebrookfunc,0.1);
What is my mistake?
Thank you.

You have to pass colebrookfunc as a function handle. Also, unless you define colebrookfunc as a nested function (which you, apparently, don't), you need to somehow pass the parameters to the function.
Thus, your call to fzero should look like:
a = fzero(#(x)colebrookfunc(x,eD,Re),0.1)
And the first line of coolebrookfunc has to be
function F = colebrookfunc(x,eD,Re)

Related

Beginner python - simple Pounds to Kilos

The instructions are very simple, but I am struggling none the less. We have learned very few things in this class so far, so I am looking for a very simplistic answer.
The instructions are as follows "Write a Python program that will prompt the user to enter a weight in pounds, then
convert it to kilograms and output the result. Note, one pound is .454 kilograms"
What i have so far is
print("Pounds to Kilos. Please Enter value in pounds.")
x=input('Pounds: ')
float(x)
print(x * .454)
You are converting the variable x's value to the float, but not assigning it to anything. So, the real value which x variable holds never changed. You did not edit the x variable in fact. You can try something like this;
print("Pounds to Kilos. Please Enter value in pounds.")
x=float(input('Pounds: '))
print(x * .454)
However, using functions in that nested manner is not recommended. Instead, initialize a new variable to hold the new float-converted value;
print("Pounds to Kilos. Please Enter value in pounds.")
x = input('Pounds: ')
x_float = float(x)
print(x_float * .454)
Going from your code example I would do something like this.
print("Pounds to Kilos. Please Enter value in pounds.")
x = float(input('Pounds: '))
print(x * 0.454, "kg")
EDIT
Maybe instead of having the calculation in the print() statement I add a separate variable for it, including the advise about float from the other solution.
print("Pounds to Kilos. Please Enter value in pounds.")
x = (input('Pounds: '))
kg = float(x) * 0.454
print(kg, "kg")
Explanation: First you ask the user's weight using input command, then you can print a message saying converting to kgs.. (it's optional). create a new variable called weight_kgs to convert the user's input to a float, then multiply the input by 0.45. after that convert the weight_kgs variable to a string once again by making a new variable called final_weight, so that you can join the input by user to a string. At the end print a message telling the user's weight. print("you weight is " + final_weight)
```
weight_lbs = input("enter your weight(lbs): ")
print("converting to kgs...")
weight_kgs = float(weight_lbs) * 0.45
final_weight = str(weight_kgs)
print("your weight is " + final_weight + "kgs") # line 5
```
I hope you got it.

Error: "can't multiply sequence by non-int of type 'float'"

It says the error is when h (altitude) is between 11000 and 25000, so I only posted the initial stuff outside all my if loops and the specific loop where the problem is happening. Here is my code:
import math;
T = 0.0;
P = 0.0;
hString = ("What is the altitude in meters?");
h = int(hString);
e = math.exp(0.000157*h);
elif 11000 < h < 25000:
T = -56.46;
P = (22.65)*[(1.73)-e];
When you use mathematical operations you need to be careful with brackets.
P = (22.65)*((1.73)-e); #will be right way of using
[ ] using will create a list which you, do not need in this program.
Here is a link which will help you learn much more about type conversions and proper use of brackets while doing mathematics on it.
Also in your code you have not used
hString =input ("What is the altitude in meters?");
h = int(hString);
input will allow you to take value from user and then int(your_input) will help you convert to integer
The square brackets in the last line ([(1.73)-e]) create a list. In this case, it's a list with one element, namely (1.73)-e. I imagine you intended those to be parens. Make that change and it will work.
The final line becomes:
P = (22.65)*((1.73)-e);

Converting a string to an expression in MATLAB

I'm writing a small MATLAB package and I'd like to ask for user input for a function. So if the user enters:
x.^2 + sin(x)
I want to use this user input to appear elsewhere in the code, but x would already be defined and so the expression above would be a vector (or scalar if length(x) is 1).
You can use the eval function for this. For example:
>> x = 5
x =
5
>> eval('x*3')
ans =
15
You can create a function handle:
% some variable you already defined
myVar = 5;
% Create an anonymous function in some z
f = str2func('#(z) z.^2 + sin(z)');
% Call function supplying the input
f(myVar)

Binary search code not working

Good afternoon everyone,
I'm trying to sort out names which are already sorted in alphabetical order. I can't figure out why my program isn't working. Any tips or pointers would be nice. Thanks.
def main():
names = ['Ava Fiscer', 'Bob White', 'Chris Rich', 'Danielle Porter', 'Gordon Pike', 'Hannah Beauregard', 'Matt Hoyle', 'Ross Harrison', 'Sasha Ricci', 'Xavier Adams']
input('Please enter the name to be searched: ', )
binarySearch
main()
def binarySearch(names):
first = 0
last = len(names) - 1
position = -1
found = False
while not found and first <= last:
middle = (first + last) / 2
if names[middle] == value:
found = True
position = middle
elif arr[middle] > value:
last = middle -1
else:
first = middle + 1
return position
What does it mean that the program isn't working? Is it a syntax error or is the problem in the wrong results?
With the code you pasted, there are several indentation problems, but besides that, lines:
input('Please enter the name to be searched: ', )
binarySearch
are also syntactically incorrect, the comma is redundant and only the function name appearing just like that is plain wrong. If you are interested in the correctness of your algorithm, it seems alright, but the boundaries can always be tricky. My code below is working and syntactically correct, if you find it helpful. (names are numbers, but that is irrelevant in this case)
names = [1,2,4,5,6,8,9]
def bs(n):
start = 0
end = len(names)
while end - start > 0:
m = (start+end)/2
if names[m] == n:
return m
elif n < names[m]:
end = m
else:
start = m + 1
return -1
print (bs(1))
print (bs(6))
print (bs(9))
print (bs(3))
print (bs(10))
print (bs(-8))
Another thing I would like to point out is that this kind of binary search is already in the python standard library, the bisect module. However, if you are writing your own for practice or for any other reason that is just fine.
if you are using python 3.* then you are going to want to change
m = (start+end)/2
to
m = (start+end)//2
When you do /2 it outputs a float in 3.*

I keep getting a syntax error

I keep getting a syntax error with the following line of code:
# If the user asked for celsius then x will be a Celsius number converting to Fahrenheit number
if x = = c
Do you have any idea?
The = = should be ==
if x == c:
Make sure your using == instead of = = (if its not clear)-> replace =[SPACE]= with ==.
Add a colon as well to removing the space.

Resources