How to apply a function to individual elements of a array/vector - arrays

I have a function f(x,a,b,c) and i want to use plot() to display it. That means i have to compute f(x) for each of my x and store them in a vector to use plot().
How can i apply my function to each element of x individually? My function requires 3 arguments aside from the value of x. I've tried arrayfun() but can't seem to get it working...
x = linspace(0.008,0.08);
a = 0.005;
b = 0.0015;
re = (1.23*40*0.005)/(1.79*10^-5);
y = arrayfun(#f, x, a, b, re);
plot(y);
Any ideas?

You could use an anonymous function:
y = arrayfun(#(x) f(x, a, b, re), x);

Related

Creating a 2D array in IDL starting from an equation

I'm trying to make a contour plot in IDL of quantity described by and equation, which here I'll take to be x^2 + y.
In order to do that, I first need to create a 2D array ("pxx").
Being a novice, I'm currently just moving my fist step into this direction and so far I've been trying to make this simpler foreach loop work:
pxx=fltarr(10, 10)
xx = indgen(10)
yy = indgen(10)
foreach k, xx do begin
pxx[k,*]=3*k
endforeach
print, pxx
But this only seems to work for the last column. Any idea on how to fix that? And how would you suggest I proceed to create a 2D array in space for the equation above?
Thank you in advance, any help is appreciated
Choose the range of x and y values you want to evaluate on:
n = 10
x = findgen(n) - (n - 1)/2.0
y = findgen(n) - (n - 1)/2.0
Expand x and y to 2-dimensional versions of themselves:
xx = rebin(reform(x, n, 1), n, n)
yy = rebin(reform(y, 1, n), n, n)
Evaluate the function:
z = xx^2 + yy
Plot:
contour, z, x, y

Matlab Array Division

I have been playing with matlab and was trying to calculate absolute relative error using two arrays. However, when I divide the two arrays, my resulting array has the same value throughout the array even though when I calculate the values by hand, they are not all the same. I was wondering why my resulting array shows the same answer for every value in the array.
Here is my code:
function [X] = absrelerror(A, B)
% Calculates absolute relative error for true value A and approximate value B.
A = linspace(sin(-pi/6), sin(pi/6), 50); %True
B = linspace(-pi/6, pi/6, 50); %Approximate
Y = abs((A-B) ./ A); %ARE equation
X = Y * 100; %convert to percent
end
I think you have approached the problem in a wrong way.
Here, the first elements of both A and B are constant. Also, the spacing between two elements of any of the vectors is also constant. Say, they are c and d where c = {A(50) - A(1)}/49 and d = {B(50) - B(1)}/49. Now, the nth value of Y is {A(1)*nc - B(1)*nd}/{A(1)*c} = {A(1)*c - B(1)*c}/A(1) which is constant. So, it's not surprising that MATLAB is giving a constant value in Y.
If I have understood correctly what you are trying to do, then you should do it in the following way:
%A = linspace(sin(-pi/6), sin(pi/6), 50); %True
B = linspace(-pi/6, pi/6, 50); %Approximate
%Y = abs((A-B) ./ A); %ARE equation
%X = Y * 100; %convert to percent
A = sin(B);
X = abs((A-B)./A) * 100;
fprintf('%f ', X)
The output is:
4.719755 4.330958 3.960262 3.607413 3.272170 2.954306 2.653606 2.369868 2.102903 1.852533 1.618593 1.400927 1.199394 1.013862 0.844209 0.690325 0.552111 0.429477 0.322344 0.230643 0.154315 0.093311 0.047592 0.017130 0.001903 0.001903 0.017130 0.047592 0.093311 0.154315 0.230643 0.322344 0.429477 0.552111 0.690325 0.844209 1.013862 1.199394 1.400927 1.618593 1.852533 2.102903 2.369868 2.653606 2.954306 3.272170 3.607413 3.960262 4.330958 4.719755

Assign Subset of Variables a Common Value via Parallel Assignment

Within Ruby, I've been searching for an elegant single-line statement to assign multiple variables (but not all) a common value returned from an array via method call.
The crux of the objective is to convert a two-line statement such as:
a, x = 1, 2
y = x
Into: a, x, y = 1, 2, 2 without hard-coding the repeated values. So a gets one value (1) while x and y both share a common value (2).
To extend the example into a use-case, let's say instead of directly assigning the values 1, 2, we assign our values via an array returned from a method call:
a, x = 7.divmod 5 # array [1,2] is unpacked via parallel assignment
y = x
This has the same result as the first code sample and we could replace the Integers with variables to make the assignment dynamic.
Can you recommend techniques to consolidate this assignment into a single line? Here are a couple options I've considered so far:
n = 7 # number
m = 5 # divided by
# Option 1) Store array via inline-variable assignment and append last
# element to align values for the parallel assignment
a, x, y = (t = n.divmod(m)) << t[-1]
# Option 2) Use Array#values_at to repeat some elements
a, x, y = n.divmod(m).values_at 0,1,1
For those new to parallel assignment, this SO post covers standard usage.
Another option
a, x, y = 7.divmod(5).tap {|a| a << a[-1]}

Skip some columns when searching a matrix in a loop

I have a matrix, c, and I want to search many times for the index of the positive minimum element
d = min(c(c>0));
[x,y] = find(c == d);
but in the next search I want it to skip the old y.
how to do it?
I want to use x and y in some other calculation.
also I want to find this d minimum just within specific columns in the matrix c like:
j from m+1 to n-1
please help
Define mask = zeros(size(c)); before the loop.
And before finding the minimum use,
newc = c + mask;
d = min(newc(newc>0));
[x,y] = find(newc == d);
mask(:,y) = NaN;
I think you can update the c matrix. I mean:
% In the loop, use it:
[x,y]=find(c==d);
c(:, y) = [];
If c matrix is important, you can use a temporary variable equals to c, instead of using c.

Function with input (a,b,c) that results in output (x,z,y)

So I'm working on a process that will allow me to calculate subobject normals for models I am putting into a game. Basically, I know how to calculate them but I need to create a function that can use this bit of code.
local sin, asin = math.sin, math.asin
local deg, rad = math.deg, math.rad
math.sin = function (x) return sin(rad(x)) end
math.asin = function (x) return asin(deg(x)) end --Makes math.sin read in degrees instead of radians
x = function (sin (a))
z = function (sin (b))
y = function (sin (c))
d = {a, b, c}
end
e = {x, z, y}
end
repeat
print("Enter a value for x:")
a = io.read("*number") -- read a number
print(math.sin(a))
print("Do you want to repeat? Type 1 to repeat")
a = io.read("*number")
until a ~= 1
The way it works is that the sine of the angle that the subobject was rotated becomes the normal value on that axis. (z = b because Blender operates in X-Y horizontal and the game is in X-Z horizontal)
Basically, I am looking for a function the allows me to input "array d" and get the output displayed as "array e" instead of that single output function. a, b, and c are variables, not constants.
A function in python can be defined as -
def myfunc(a,b,c):
x = sin (a)
z = sin (b)
y = sin (c)
return x,y,z
or you can input an array -
def myfunc(arr):
x = sin (arr[0])
z = sin (arr[1])
y = sin (arr[2])
return {x,y,z}
Notice that you can return multiple numbers stored in different ways, depending on your needs.

Resources