I want to define the mathematical function sum(x^z)+z where x ranges from 1:10 and z is a variable
One way I tried is
syms z
f=#(z)(z)
for i=1:10
f=f+(i.^z)
end
But this leads to a error: binary operator '+' not implemented for 'function handle' by 'matrix' operations
I also tried to define a function initially of two variables x and z as
syms x
syms z
f(x,z)=z+(x.^z)
sum(f([1:10,z]))
It also gives error
Please tell me the correct method to do this.
Related
error: 'y' undefined near line 8 column 12
error: called from computeCost at line 8 column 3
Here is my code:
1;
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = sum(( X * theta - y ) .^2 )/( 2 * m );
% =========================================================================
end
I am guessing it's an error from Coursera ML course assignment. I think you are trying to run the file which contains the implementation of function computeCost(X, y, theta), not the file which calls the computeCost(,,) function with values of X, y, theta. This is why you are getting the error as you aren't providing y.
Run the file which is calling computeCost() function, not the file which contains the implementation of computeCost() function.
That is:
For Week2 Assignment 1: Run ex1.m file
For Week3 Assignment 2: Run ex2.m file
There are two things happening here. First you are defining your function dynamically as opposed to in its own file; not sure why you would prefer that.
Second, after having defined this computeCost function, you are calling it from a context where you did not pass a y argument (or presumably, you didn't pass any arguments to it, and y happens to be the first one detected as missing inside the function).
Since this is a cost function and your code looks suspiciously like code from Andrew Ng's Machine Learning course on Coursera, I am going to go out on a limb here and guess that you called computeCost from something else that was supposed to use it as a cost function to be optimised, e.g. fminunc. Typically functions like fminunc expect a function handle as an argument, but they expect a very specific function handle too. If you look at the help of fminunc, it states that:
FCN should accept a vector (array) defining the unknown variables,
and return the objective function value, optionally with gradient.
Therefore, if you want to pass a function that is to be computed with three arguments, you need to "wrap" it into your own handle, which you can define on the spot, e.g. #(x) computeCost(x, y, t) (assuming 'y' and 't' exist already).
So, I'm guessing that instead of calling fminunc like so: fminunc( #(x) computeCost(x, y, t),
you probably called it like so: fminunc( #computeCost )
or even like so: fminunc( computeCost ) (which evaluates the function first, rather than pass a function handle as an argument).
Basically, go back to the code given to you by coursera, or read the notes carefully. You're calling things the wrong way.
Actually, you are trying to run a function and you can't run it until you provide the desired no. of parameters. Doing so, you may encounter the following error:
computeCost error: 'y' undefined near line 7 column 12
error: called from computeCost at line 7 column 3
As you see, here I'm calling this function without passing any argument.
SOLUTION:
You can test your code by running 'ex1' script. After that submit your work by calling 'submit' script.
Basically, I need to solve a function, ill use x^2 * y^3 for an example, but using the x and y values over a range of (0,2), with different steps for x values and for y values. For example, if the step was 1 for both x and y, I would just solve the function for (0,0),(0,2),(2,0), and (2,2).
I'm not sure how to accomplish this in Fortran without using arrays. I was thinking about using a "DO" and then solving the function for all of the initial "y" values, while using the correct amount of steps for "x," but im not sure how I would get the second "y" value with the "x" values reset so that the "y" can be calculated with all of the x values.
Thank you!
Here is a simple solution, based on do concurrent construct in modern Fortran (>2008) which is guaranteed to be equally or more efficient than the conventional do-loops in Fortran:
program hello
integer :: x,y
real :: stepSizeX = 0.1, stepSizeY = 0.2
do concurrent(x=0:2:1,y=0:2:1)
write(*,*) (stepSizeX*x)**2 * (stepSizey*y)**3
end do
end program Hello
You can test it here (set the compiler's Fortran standard to -std=f2008 or -std=gnu).
Let us suppose that we run the following set of commands in Octave:
pkg load symbolic %loads the symbolic math package
syms x y %declare x and y symbols
f = x^2 - 2*x + 3;
V = [-5:0.25:5]';
V_x = subs(f, x, V)
At this point V_x is a symbolic expression in Octave. Now, if this were to be MATLAB, one would run eval(V_x) and everything would be converted to numbers. However, eval does not seem to run in Octave as in MATLAB.
What should be done to convert the symbolic array into numbers?
double has been overloaded for symbolic variables so you can use double to explicitly convert the symbolic result to it's numeric representation
V_x_num = double(V_x);
This works in MATLAB as well as Octave.
and how about getting variable precision (number of digits) in the evaluated symbolic output, ie, staying in the vpa symbolic space but solving all the sym internal functions to digits
eval still outputs in the default octave output_precision and format long limitations, so that is of no use.
this is one way (sym variable y holds the value):
sympref digits 1000
syms x
eqn = x==y
vpasolve(eqn)
I wrote this code in octave:
syms z;
f=z-2;
fsolve("f",0.)
Then this gives the error
#f: no function and no method found.
Also using fsolve(#f,0) gives the same error
When I write code as:
syms z;
f=z-2;
fsolve(f,0.)
Then this gives the error
ind2sub: subscript indices must be either positive integers less than 2^31 or logicals.
Please explain to me how to actually use fsolve.
% syms z; % Not needed, actually slows down the code
f=#(z)(z-2);
fsolve(f,0.)
You're missing the # symbol, which is a function handle. This tells Octave that f is not a variable, but actually is a(n anonymous) function, in this case of z, which is the first argument.
You'll probably want to have z to be a regular variable, because making it symbolic turns MATLAB from a speeding race car to a drudging farm vehicle. Unless there's a specific reason to have z symbolic (I cant think of any in case of usage with fsolve)' it's best to avoid symbolic maths.
I am trying to understand what's useful and how to actually use lambda expression in Haskell.
I don't really understand the advantage of using lambda expression over the convention way of defining functions.
For example, I usually do the following:
let add x y = x+y
and I can simply call
add 5 6
and get the result of 11
I know I can also do the following:
let add = \x->(\y-> x+y)
and get the same result.
But like I mentioned before, I don't understand the purpose of using lambda expression.
Also, I typed the following code (a nameless function?) into the prelude and it gave me an error message.
let \x -> (\y->x+y)
parse error (possibly incorrect indentation or mismatched backets)
Thank you in advance!
Many Haskell functions are "higher-order functions", i.e., they expect other functions as parameters. Often, the functions we want to pass to such a higher-order function are used only once in the program, at that particular point. It's simply more convenient then to use a lambda expression than to define a new local function for that purpose.
Here's an example that filters all even numbers that are greater than ten from a given list:
ghci> filter (\ x -> even x && x > 10) [1..20]
[12,14,16,18,20]
Here's another example that traverses a list and for every element x computes the term x^2 + x:
ghci> map (\ x -> x^2 + x) [1..10]
[2,6,12,20,30,42,56,72,90,110]