numerical-methods

How to calculate machine epsilon in MATLAB?

牧云@^-^@ 提交于 2019-11-28 04:16:03
问题 I need to find the machine epsilon and I am doing the following: eps = 1; while 1.0 + eps > 1.0 do eps = eps /2; end However, it shows me this: Undefined function or variable 'do'. Error in epsilon (line 3) while 1.0 + eps > 1.0 do What should I do? 回答1: First and foremost, there is no such thing as a do keyword in MATLAB, so eliminate that from your code. Also, don't use eps as an actual variable. This is a pre-defined function in MATLAB that calculates machine epsilon, which is also what

Jacobi iteration doesn't end

╄→尐↘猪︶ㄣ 提交于 2019-11-28 01:43:02
问题 I'm trying to implement the Jacobi iteration in MATLAB but am unable to get it to converge. I have looked online and elsewhere for working code for comparison but am unable to find any that is something similar to my code and still works. Here is what I have: function x = Jacobi(A,b,tol,maxiter) n = size(A,1); xp = zeros(n,1); x = zeros(n,1); k=0; % number of steps while(k<=maxiter) k=k+1; for i=1:n xp(i) = 1/A(i,i)*(b(i) - A(i,1:i-1)*x(1:i-1) - A(i,i+1:n)*x(i+1:n)); end err = norm(A*xp-b);

Power Method in MATLAB

元气小坏坏 提交于 2019-11-28 01:37:29
问题 I would like to implement the Power Method for determining the dominant eigenvalue and eigenvector of a matrix in MATLAB. Here's what I wrote so far: %function to implement power method to compute dominant %eigenvalue/eigenevctor function [m,y_final]=power_method(A,x); m=0; n=length(x); y_final=zeros(n,1); y_final=x; tol=1e-3; while(1) mold=m; y_final=A*y_final; m=max(y_final); y_final=y_final/m; if (m-mold)<tol break; end end end With the above code, here is a numerical example: A=[1 1 -2;-1

Implementing the derivative in C/C++

一世执手 提交于 2019-11-27 18:34:46
How is the derivative of a f(x) typically calculated programmatically to ensure maximum accuracy? I am implementing the Newton-Raphson method, and it requires taking of the derivative of a function. John D. Cook I agree with @erikkallen that (f(x + h) - f(x - h)) / 2 * h is the usual approach for numerically approximating derivatives. However, getting the right step size h is a little subtle. The approximation error in ( f(x + h) - f(x - h)) / 2 * h decreases as h gets smaller, which says you should take h as small as possible. But as h gets smaller, the error from floating point subtraction

How do I determine the coefficients for a linear regression line in MATLAB? [closed]

大兔子大兔子 提交于 2019-11-27 15:25:12
I'm going to write a program where the input is a data set of 2D points and the output is the regression coefficients of the line of best fit by minimizing the minimum MSE error. I have some sample points that I would like to process: X Y 1.00 1.00 2.00 2.00 3.00 1.30 4.00 3.75 5.00 2.25 How would I do this in MATLAB? Specifically, I need to get the following formula: y = A + Bx + e A is the intercept and B is the slope while e is the residual error per point. Judging from the link you provided, and my understanding of your problem, you want to calculate the line of best fit for a set of data

Fast gradient-descent implementation in a C++ library?

♀尐吖头ヾ 提交于 2019-11-27 14:05:19
问题 I'm looking to run a gradient descent optimization to minimize the cost of an instantiation of variables. My program is very computationally expensive, so I'm looking for a popular library with a fast implementation of GD. What is the recommended library/reference? 回答1: GSL is a great (and free) library that already implements common functions of mathematical and scientific interest. You can peruse through the entire reference manual online. Poking around, this starts to look interesting, but

How to find a binary logarithm very fast? (O(1) at best)

亡梦爱人 提交于 2019-11-27 13:55:19
问题 Is there any very fast method to find a binary logarithm of an integer number? For example, given a number x=52656145834278593348959013841835216159447547700274555627155488768 such algorithm must find y=log(x,2) which is 215. x is always a power of 2. The problem seems to be really simple. All what is required is to find the position of the most significant 1 bit. There is a well-known method FloorLog, but it is not very fast especially for the very long multi-words integers. What is the

argsort for a multidimensional ndarray

半腔热情 提交于 2019-11-27 13:45:40
I'm trying to get the indices to sort a multidimensional array by the last axis, e.g. >>> a = np.array([[3,1,2],[8,9,2]]) And I'd like indices i such that, >>> a[i] array([[1, 2, 3], [2, 8, 9]]) Based on the documentation of numpy.argsort I thought it should do this, but I'm getting the error: >>> a[np.argsort(a)] IndexError: index 2 is out of bounds for axis 0 with size 2 Edit: I need to rearrange other arrays of the same shape (e.g. an array b such that a.shape == b.shape ) in the same way... so that >>> b = np.array([[0,5,4],[3,9,1]]) >>> b[i] array([[5,4,0], [9,3,1]]) Solution: >>> a[np

MATLAB - Fit exponential curve WITHOUT toolbox

一曲冷凌霜 提交于 2019-11-27 08:36:34
问题 I want to fit a decaying exponential to the plotted data. I do NOT have the Curve Fitting or Optimization Toolboxes. x = [0 0.0036 0.0071 0.0107 0.0143 0.0178 0.0214 0.0250 0.0285 0.0321 0.0357 0.0392 0.0428 0.0464 0.0464]; y = [1.3985 1.3310 1.2741 1.2175 1.1694 1.1213 1.0804 1.0395 1.0043 0.9691 0.9385 0.9080 0.8809 0.7856 0.7856]; figure() plot(x,y,'*') How could I achieve this in MATLAB? 回答1: Assuming that you have Gaussian distributed errors between the input and output points, and

Finding solution to Cauchy prob. in Matlab

穿精又带淫゛_ 提交于 2019-11-27 08:27:55
问题 I need some help with finding solution to Cauchy problem in Matlab. The problem: y''+10xy = 0, y(0) = 7, y '(0) = 3 Also I need to plot the graph. I wrote some code but, I'm not sure whether it's correct or not. Particularly in function section. Can somebody check it? If it's not correct, where I made a mistake? Here is separate function in other .m file: function dydx = funpr12(x,y) dydx = y(2)+10*x*y end Main: %% Cauchy problem clear all, clc xint = [0,5]; % interval y0 = [7;3]; % initial