I have made a function with mathematica and Im getting what I want from it however I am also getting 'Null' on the end and im not sure why.
Here is my code:
x = ""
ButterflyString[y_]:=For[i = 1, i < 8, i++, x = StringTake[y, {i}] <> x] x
My input:
ButterflyString["Wolfram"]
My output:
marfloW Null
As #Bill posted in the comments:
x = "";
ButterflyString[y_] :=
(For[i = 1, i < 8, i++, x = StringTake[y, {i}] <> x];
x)
This worked for me.
Related
In Octave why does my function here always return 1?
Code:
function val = g2(x)
sigma = 0.1;
if (x <= -8)
val = exp(-0.5*((x+8)^2)/(sigma^2));
endif
if (x >= 8)
val = exp(-0.5*((x-8)^2)/(sigma^2));
endif
if (-8 < x < 8)
val = 1;
endif
endfunction
-8 < x < 8 is evaluated as (-8 < x) < 8, which is either true < 8 or false < 8, depending on the value of x. Both of which are true, because false is 0 in a numerical situation, and true is 1. So the last conditional statement is always executed. In Octave you’d write (-8 < x) && (x < 8).
But better would be to write your code as
function val = g2(x)
sigma = 0.1;
if x <= -8
val = exp(-0.5*((x+8)^2)/(sigma^2));
elseif x >= 8
val = exp(-0.5*((x-8)^2)/(sigma^2));
else
val = 1;
endif
endfunction
I was trying to run the program but the window says
error: 'a' undefined near line 2, column 10
error: called from
false_position at line 2 column 6
here's my code
function y = false_position(f, a, b, error)
if ~(f(a) < 0)
disp("f(a) must be less than 0")
elseif ~(f(b) > 0)
disp("f(b) must be greater than zero")
else
c = 100000;
while abs(f(c)) > error
%Formula for the x-intercept
c = -f(b) * (b - a) / (f(b) - f(a)) + b;
if f(c) < 0
a = c;
else
b = c;
endif
disp(f(c))
endwhile
x = ["The root is approximately located at ", num2str(c)];
disp(x)
y = c;
endif
endfunction
every time i ran the code, it says like that and i am not really a pro with using the octave. I was kinda hoping someone will help me with this error.
Any answers will do
I'm trying to plot a piecewise function in R that is equal to -3 if x < 0, 1/3 * x^3 if x is between 0 and 5 (inclusive), and 4x otherwise. The function I've been running seems to work, but the plot returns an error message. The code I wrote is:
g <- function(x) {
if (x < 0)
-3 # first component
else if (x >= 0 & x <= 5)
(1/3) * x #second component
else
4*x # third component
}
Then when I try to plot it with
plot(g, -20, 20)
I get
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' did not evaluate to an object of length 'n'
In addition: Warning message:
In if (x < 0) -3 else if (x >= 0 & x <= 5) (1/3) * x else 4 * x :
the condition has length > 1 and only the first element will be used
I can't quite figure out how to troubleshoot this, and the 'ifelse' function returned a dark black line when I attempted to plot it, which I'd prefer to avoid, so this seems to be my best bet.
I would greatly appreciate any insights.
I wrote a simple code in Octave,but it keeps reporting parse error which I can't find.The code is
X = magic(3)
m = size(X, 1)
p = zeros(m, 1)
theta = [1;2;1]
hypo = 1./(1.+exp(-X*theta));
for i = 1:m
if hypo(i) > 0.5
p(i) = 1;
else
p(i) = 0;
end
and Octave reports
parse error near line 12 of file F:/my document/machine learning/machine-learning-ex2/ex2/new 1.m
syntax error
error: source: error sourcing file 'F:/my document/machine learning/machine-learning-ex2/ex2/new 1.m'
error: parse error
But,there is nothing in line 12.The last line is 11.I don't know where is wrong.
You're missing an end to terminate the if statement. The correct code should be like this:
X = magic(3)
m = size(X, 1)
p = zeros(m, 1)
theta = [1;2;1]
hypo = 1./(1.+exp(-X*theta));
for i = 1:m
if hypo(i) > 0.5
p(i) = 1;
else
p(i) = 0;
end % <-- you forgot this!
end
The if statement end with endif in octave (see: https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html), and also the for statement with endfor (see: https://www.gnu.org/software/octave/doc/interpreter/The-for-Statement.html)
So the correct code would be:
X = magic(3)
m = size(X, 1)
p = zeros(m, 1)
theta = [1;2;1]
hypo = 1./(1.+exp(-X*theta));
for i = 1:m
if hypo(i) > 0.5
p(i) = 1;
else
p(i) = 0;
endif
endfor
I want to build a function in the following way:
f[x_] := 0;
For[i = 1, i <= n, i++,
g[x_] := 0;
For[k = 1, k <= i, k++,
g ^:= g[#]*(# - X[[k]]) &;
g[x_] = g;
]
f ^:= f[#] + Q[[1, i]]*g[#];
f[x_] = f;
So I get a polynomial
Q_11 * (x-x_1) + Q_12 * (x-x_1) * (x-2x_2) ...
This is the latest version I have, but it does not work. The problem is that I have to add functions (I found some posts where it is done) but I have to save the new function so I can use it again.
Does someone know what to do?
Thanks!
Marius
I cant make head or tail of your code but you can readily construct that result just like this:
qlist = Array[q, {10, 10}];
xlist = Array[x, 10];
poly[n_] := Sum[ qlist[[1, j]] Product[ (x - xlist[[i]] ), {i, j}] , {j, 3}]
poly[3]
q[1, 1] (x - x[1]) + q[1, 2] (x - x[1]) (x - x[2]) +
q[1, 3] (x - x[1]) (x - x[2]) (x - x[3])
If you really need help using UpSetDelayed you should pose a more simple / specific question ( ie without the For loops. )