<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8653762156638787988</id><updated>2024-09-05T04:58:27.104+02:00</updated><category term="HaskNum"/><category term="BisectionMethod"/><category term="Fixed-Point-Iteration"/><category term="Newton Method"/><category term="Secant Method"/><category term="cross-compile"/><category term="freebsd"/><category term="ports"/><title type='text'>Monthadar</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-2791128793410922208</id><published>2012-06-23T21:00:00.000+02:00</published><updated>2012-06-23T21:00:20.356+02:00</updated><title type='text'>Xen - host Arch Linux - guest Arch Linux</title><content type='html'>This is a small guide that tries to show you how I installed Xen and run Arch Linux as a guest on my host Arch Linux.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You are expected to install Arch Linux. The &lt;a href=&quot;https://wiki.archlinux.org/&quot;&gt;wiki&lt;/a&gt; is your resource. And for Xen this &lt;a href=&quot;https://wiki.archlinux.org/index.php/Xen&quot;&gt;wiki&lt;/a&gt; is your main resource.&lt;br /&gt;
&lt;br /&gt;
But the Xen package seems to be out of date, luckely one of the comment gives a &lt;a href=&quot;https://bitbucket.org/encbladexp/aur/src&quot;&gt;link&lt;/a&gt; for a more up to date source. So I download the source:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;cd ~/builds
hg clone https://bitbucket.org/encbladexp/aur&lt;/pre&gt;
&lt;br /&gt;
Then I followed the &lt;a href=&quot;https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages&quot;&gt;AUR wiki installing packages&lt;/a&gt; to install Xen.&lt;br /&gt;
&lt;br /&gt;
When it is installed, you need to make some adjustments to both Grub boot list in /boot/grub/menu.lst (I asume your using Grub) and /etc/rc.conf. The changes needed is found in the file ~/builds/aur/xen/xen.install.&lt;br /&gt;
&lt;br /&gt;
But these changes didnt work for me, I needed to modify menu.lst:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;# (2) Xen
title Xen with Arch Linux
root (hd0,0)
kernel /boot/xen.gz dom0_mem=512M dom0_max_vcpus=4 dom0_vcpus_pin
module /boot/vmlinuz-linux root=/dev/sdaXX ro
module /boot/initramfs-linux.img&lt;/pre&gt;
&lt;br /&gt;
As you can compare I changed the &quot;root=&quot; option (Note /dev/sdaXX is just an example. This is the parition that your HOST OS uses and not the GUEST OS see below where we format a new empty partition). Now after reboot choose &quot;Xen with Arch Linux&quot; from your Grub menu.
&lt;br /&gt;
Next I tried to setup a guest Arch Linux in Xen, this is called domU (U for User). I struggeled with this step, things seems to be scattered all over the wiki page and some is out dated.
So this is my attempt to organize this process.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;## /dev/sdbXX is an example of a block device
$ mkfs.ext4 /dev/sdbXX    ## format partition
$ mkdir /tmp/install
$ mount /dev/sdbXX /tmp/install
$ mkdir -p /tmp/install/{dev,proc,sys} /tmp/install/var/lib/pacman /tmp/install/var/cache/pacman/pkg
$ mount -o bind /dev /tmp/install/dev
$ mount -t proc none /tmp/install/proc
$ mount -o bind /sys /tmp/install/sys
$ pacman -Sy -r /tmp/install --cachedir /tmp/install/var/cache/pacman/pkg -b /tmp/install/var/lib/pacman base
$ cp -r /etc/pacman* /tmp/install/etc
$ chroot /tmp/install /bin/bash
$ vi /etc/fstab
    /dev/xvda               /           ext4    defaults                0       1
# Find and uncomment all 6 console and add one as follows:
$ vi /etc/inittab
    c1:2345:respawn:/sbin/agetty -8 38400 hvc0 linux
    #c1:2345:respawn:/sbin/agetty -8 38400 tty1 linux
    #c2:2345:respawn:/sbin/agetty -8 38400 tty2 linux
    #c3:2345:respawn:/sbin/agetty -8 38400 tty3 linux
    #c4:2345:respawn:/sbin/agetty -8 38400 tty4 linux
    #c5:2345:respawn:/sbin/agetty -8 38400 tty5 linux
    #c6:2345:respawn:/sbin/agetty -8 38400 tty6 linux 
$ exit  ## exit chroot
$ umount /tmp/install/dev
$ umount /tmp/install/proc
$ umount /tmp/install/sys
$ umount /tmp/install
$ vi /etc/xen/dom01     ## create config file
    #  -*- mode: python; -*-
    kernel = &quot;/boot/vmlinuz-linux&quot;
    ramdisk = &quot;/boot/xen-initramfs-linux.img&quot;
    memory = 256
    name = &quot;dom01&quot;
    vif = [ &#39;mac=00:16:3e:00:01:01&#39; ]
    disk = [ &#39;phy:/dev/sdaXX,xvda,w&#39; ]
    dhcp=&quot;dhcp&quot;
    hostname = &quot;ooga&quot;
    root = &quot;/dev/xvda ro&quot;
&lt;/pre&gt;
Now we have a working filesystem for the guest OS, usuable kernel from the host system (you can compile your own custom kernel if you like). But because we use the same kernel we need to load some Xen specific modules early on for the guest OS to boot.
This is done by a tool called mkinitcpio. This tool needs some configuration files. You can modify the ones that comes with Arch Linux but I copied the files and modified them. These are the files I copied and edited:
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;$ cat /etc/mkinitcpio.d/xen-linux.preset
# mkinitcpio preset file for the &#39;linux&#39; package
    
ALL_config=&quot;/etc/mkinitcpio.conf&quot;
ALL_kver=&quot;/boot/vmlinuz-linux&quot;
    
PRESETS=(&#39;default&#39; &#39;fallback&#39;)

default_config=&quot;/etc/xen-mkinitcpio.conf&quot;
default_image=&quot;/boot/xen-initramfs-linux.img&quot;
#default_options=&quot;&quot;
    
fallback_config=&quot;/etc/xen-mkinitcpio.conf&quot;
fallback_image=&quot;/boot/xen-initramfs-linux-fallback.img&quot;
fallback_options=&quot;-S autodetect&quot;
&lt;/pre&gt;
&lt;pre class=&quot;codeview&quot;&gt;$ cat /etc/xen-mkinitcpio.conf
# vim:set ft=sh
# MODULES
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES=&quot;piix ide_disk reiserfs&quot;
MODULES=&quot;xen-blkfront xen-fbfront xenfs xen-netfront xen-kbdfront&quot;

# BINARIES
# This setting includes any additional binaries a given user may
# wish into the CPIO image.  This is run last, so it may be used to
# override the actual binaries included by a given hook
# BINARIES are dependency parsed, so you may safely ignore libraries
BINARIES=&quot;&quot;

# FILES
# This setting is similar to BINARIES above, however, files are added
# as-is and are not parsed in any way.  This is useful for config files.
# Some users may wish to include modprobe.conf for custom module options
# like so:
#    FILES=&quot;/etc/modprobe.d/modprobe.conf&quot;
FILES=&quot;&quot;

# HOOKS
# This is the most important setting in this file.  The HOOKS control the
# modules and scripts added to the image, and what happens at boot time.
# Order is important, and it is recommended that you do not change the
# order in which HOOKS are added.  Run &#39;mkinitcpio -H &lt;hook name=&quot;&quot;&gt;&#39; for
# help on a given hook.

# &#39;base&#39; is _required_ unless you know precisely what you are doing.
# &#39;udev&#39; is _required_ in order to automatically load modules
# &#39;filesystems&#39; is _required_ unless you specify your fs modules in MODULES
# Examples:
##   This setup specifies all modules in the MODULES setting above.
##   No raid, lvm2, or encrypted root is needed.
#    HOOKS=&quot;base&quot;
#
##   This setup will autodetect all modules for your system and should
##   work as a sane default
#    HOOKS=&quot;base udev autodetect pata scsi sata filesystems&quot;
#
##   This is identical to the above, except the old ide subsystem is
##   used for IDE devices instead of the new pata subsystem.
#    HOOKS=&quot;base udev autodetect ide scsi sata filesystems&quot;
#
##   This setup will generate a &#39;full&#39; image which supports most systems.
##   No autodetection is done.
#    HOOKS=&quot;base udev pata scsi sata usb filesystems&quot;
#

##   This setup assembles a pata mdadm array with an encrypted root FS.
##   Note: See &#39;mkinitcpio -H mdadm&#39; for more information on raid devices.
#    HOOKS=&quot;base udev pata mdadm encrypt filesystems&quot;
#
##   This setup loads an lvm2 volume group on a usb device.
#    HOOKS=&quot;base udev usb lvm2 filesystems&quot;
#
##   NOTE: If you have /usr on a separate partition, you MUST include the
#    usr, fsck and shutdown hooks.
HOOKS=&quot;base udev autodetect pata scsi sata filesystems usbinput fsck&quot;

# COMPRESSION
# Use this to compress the initramfs image. By default, gzip compression
# is used. Use &#39;cat&#39; to create an uncompressed image.
#COMPRESSION=&quot;gzip&quot;
#COMPRESSION=&quot;bzip2&quot;
#COMPRESSION=&quot;lzma&quot;
#COMPRESSION=&quot;xz&quot;
#COMPRESSION=&quot;lzop&quot;

# COMPRESSION_OPTIONS
# Additional options for the compressor
#COMPRESSION_OPTIONS=&quot;&quot;
&lt;/hook&gt;&lt;/pre&gt;
Then run
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;$ mkinitcpio -p xen-linux
&lt;/pre&gt;
&lt;br /&gt;
This will create an initial filesystem on ram-memory that is loaded before your filesystem and makes it possible for the guest OS to boot.

Now you can boot the guest OS with
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;$ xm create -c dom01&lt;/pre&gt;
And you should see it boot with:
&lt;br /&gt;
&lt;pre class=&quot;codeview&quot;&gt;Using config file &quot;/etc/xen/dom01&quot;.
Started domain dom01 (id=24)
                            [    0.076618] i8042: No controller found
[    0.137323] drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
:: running early hook [udev]
:: running hook [udev]
:: Triggering uevents...
:: performing fsck on &#39;/dev/xvda&#39;
/dev/xvda: clean, 27237/2564096 files, 349439/10240000 blocks
:: mounting &#39;/dev/xvda&#39; on real root
:: running cleanup hook [udev]
INIT: version 2.88 booting
 
 &amp;amp;gt; Arch Linux
 
 &amp;amp;gt; http://www.archlinux.org


Arch Linux 3.4.3-1-ARCH (hvc0)

(none) login:&lt;/pre&gt;
Have fun!</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/2791128793410922208/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2012/06/xen-host-arch-linux-guest-arch-linux.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/2791128793410922208'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/2791128793410922208'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2012/06/xen-host-arch-linux-guest-arch-linux.html' title='Xen - host Arch Linux - guest Arch Linux'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-4512019912402816899</id><published>2012-01-03T19:41:00.000+01:00</published><updated>2012-01-03T22:18:53.411+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cross-compile"/><category scheme="http://www.blogger.com/atom/ns#" term="freebsd"/><category scheme="http://www.blogger.com/atom/ns#" term="ports"/><title type='text'>Whats with FreeBSD and cross compiling?</title><content type='html'>&lt;p&gt;From the beginning me and FreeBSD didn&#39;t go along very well when I was working on RouterStation Pro. Maybe it was because of my approach to cross-compiling. In my head I want to do &lt;span style=&quot;font-weight:bold;&quot;&gt;everything&lt;/span&gt; on the host computer and then &quot;deploy&quot; it on the target.&lt;/p&gt;&lt;p&gt;Now cross compiling the kernel and world did work out for me in the end, and boy do I love how it works:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;make TARGET=mips TARGET_ARCH=mipseb buildworld&lt;br /&gt;make TARGET=mips TARGET_ARCH=mipseb KERNCONF=AR71XX buildkernel&lt;/pre&gt;&lt;p&gt; Ok, I still hate the fact that I need to tell FreeBSD explicitly to not clean-up before compiling again when I just did a small change in the kernel, added a printf for instance? So then I change the buildkernel command to:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;make TARGET=mips TARGET_ARCH=mipseb KERNFAST=AR71XX buildkernel&lt;/pre&gt;&lt;p&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;KERNFAST&lt;/span&gt; will just recompile the modified folder and then link everything, so why don&#39;t FreeBSD have KERNFAST as default?&lt;/p&gt;&lt;p&gt;Well you will get used to that eventually, so now we have a compiled kernel and world (I am not going deep into this, how do the kernel boot? What about the boot loader? etc..). Ok, now if I want rsync on my target what is the next step?&lt;/p&gt;&lt;p&gt;rsync is a third-party program residing under the /usr/ports folder, and somehow its not so easy to cross-compile. Why? I don&#39;t know, there is surely good reasons and one need to study the ports framework. But hasn&#39;t that bother anyone?&lt;/p&gt;&lt;p&gt;One day I hope to find an easy way to cross-compile ports, something like:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;cd /usr/ports/somewhere&lt;br /&gt;make TARGET=mips TARGET_ARCH=mipseb install&lt;/pre&gt;&lt;p&gt;Lets say that will install somewhere in /usr/obj/mips.mipseb/... if so then I will die a happy man =)&lt;/p&gt;&lt;p&gt;While we are wishful, it would be nice to have things like:&lt;p&gt;&lt;pre class=&quot;codeview&quot;&gt;PORTS_OVERRIDE=...&lt;br /&gt;WITHOUT_PORTS=...&lt;br /&gt;make buildports&lt;br /&gt;make installports&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;just things nice to have, why not add a ports configuration file? =D&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/4512019912402816899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2012/01/whats-with-freebsd-and-cross-compiling.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/4512019912402816899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/4512019912402816899'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2012/01/whats-with-freebsd-and-cross-compiling.html' title='Whats with FreeBSD and cross compiling?'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-3199641397629505659</id><published>2010-02-16T22:00:00.000+01:00</published><updated>2010-02-16T22:23:44.988+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BisectionMethod"/><category scheme="http://www.blogger.com/atom/ns#" term="Fixed-Point-Iteration"/><category scheme="http://www.blogger.com/atom/ns#" term="HaskNum"/><category scheme="http://www.blogger.com/atom/ns#" term="Newton Method"/><category scheme="http://www.blogger.com/atom/ns#" term="Secant Method"/><title type='text'>Code rewrite!!</title><content type='html'>&lt;p&gt;Hi again! I was searching the web for other people writing numerical solvers in Haskell and found this nice &lt;a href=&quot;http://flowerb0x.livejournal.com/1093.html&quot;&gt;guy&lt;/a&gt; called James. His idea is to &lt;span style=&quot;font-weight:bold;&quot;&gt;extract the tolerance&lt;/span&gt; test to a new function. This function takes an infinite list of approximated x values and then take the value that gives a relative error less than the specified tolerance.&lt;/p&gt;&lt;p&gt;Now relative error is calculated as absolute value of (x(n-1)-x(n))/x(n-1). I changed his convergence function and corrected a mistake in it, cause he forgot to take the absolute value for the whole expression of relative error. Here is my code:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;module HaskNum where&lt;br /&gt;&lt;br /&gt;converge :: (Ord a, Fractional a) =&gt; a -&gt; [a] -&gt; a&lt;br /&gt;converge tol (old:new:xs) = if reldiff old new &lt;= tol&lt;br /&gt;                          then new&lt;br /&gt;                          else converge tol (new:xs)&lt;br /&gt;    where reldiff old new = abs $ (old-new) / old&lt;br /&gt;converge tol (x:_) = x -- to handle when some functions&lt;br /&gt;                       -- succeed from the first time&lt;br /&gt;&lt;/pre&gt;&lt;p&gt; What I did was put it in a module I call HaskNum that will grow with time. And here is the new version of my previous four functions:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;bisec :: (F -&gt; F) -&gt; (F,F) -&gt; [F]&lt;br /&gt;bisec f (a, b)&lt;br /&gt; | f(c) == 0 = [c]&lt;br /&gt; | f(a)*f(c) &lt; 0 = c : bisec f (a,c)&lt;br /&gt; | otherwise = c : bisec f (c,b)&lt;br /&gt;  where c = (a+b)/2 &lt;br /&gt;&lt;/pre&gt;&lt;pre class=&quot;codeview&quot;&gt;fpi :: (F -&gt; F) -&gt; F -&gt; [F]&lt;br /&gt;fpi = iterate&lt;br /&gt;&lt;/pre&gt;&lt;pre class=&quot;codeview&quot;&gt;newton :: (F -&gt; F) -&gt; (F -&gt; F) -&gt; F -&gt; [F]&lt;br /&gt;newton f f&#39; = iterate xc&lt;br /&gt; where xc xi = xi - f(xi)/f&#39;(xi)&lt;br /&gt;&lt;/pre&gt;&lt;pre class=&quot;codeview&quot;&gt;secant :: (F -&gt; F) -&gt; (F, F) -&gt; [F]&lt;br /&gt;secant f (x0, x1) = xc : secant f (x1, xc)&lt;br /&gt; where xc = x1 - f(x1)*(x1-x0)/(f(x1)-f(x0))&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;Now things look really nice! And oh here how u can call one of them with a function that u define:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;main = do&lt;br /&gt; let answer = (converge 0.0000005 . bisec (func)) (0,1)&lt;br /&gt; print answer&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;An interesting note is that all the methods except the Bisection Method gives the answer xc=0.6823278. The Bisection Method gives 0.682328, i.e. one digit less. Now that is a mystery for me, but I would guess its something with computer arithmetic and the intermediate operations.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/3199641397629505659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2010/02/hi-again-i-was-searching-web-for-other.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/3199641397629505659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/3199641397629505659'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2010/02/hi-again-i-was-searching-web-for-other.html' title='Code rewrite!!'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-2275032227770315047</id><published>2010-01-22T17:00:00.000+01:00</published><updated>2010-01-23T13:03:12.217+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="HaskNum"/><category scheme="http://www.blogger.com/atom/ns#" term="Newton Method"/><category scheme="http://www.blogger.com/atom/ns#" term="Secant Method"/><title type='text'>Newton &amp; Secant Methods</title><content type='html'>&lt;p&gt;Been a while since I added new methods, but here comes two.&lt;/p&gt;&lt;p&gt;The first one is called &lt;span style=&quot;font-weight:bold;&quot;&gt;Newton&#39;s method&lt;/span&gt;. Basically it will &quot;draw&quot; a tangent that goes through the initial guess. Then in the next iteration it will draw a second tangent for the x point where the first tangent crossed the x-axis and so on. You need to supply it an initial guess.&lt;/p&gt;&lt;p&gt;The formula is x = x0 - f(x0)/f&#39;(x0).&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;type F = Float&lt;br /&gt;newton :: (F -&gt; F) -&gt; (F -&gt; F) -&gt; F -&gt; Integer -&gt; [F]&lt;br /&gt;newton _ _ _ 0 = []&lt;br /&gt;newton f f&#39; xi k = xc : newton f f&#39; xc (k-1)&lt;br /&gt;    where xc = xi - f(xi)/f&#39;(xi)&lt;br /&gt;&lt;br /&gt;func :: F -&gt; F&lt;br /&gt;func x = x^3+x-1&lt;br /&gt;&lt;br /&gt;func&#39; :: F -&gt; F&lt;br /&gt;func&#39; x = 3*x^2+1&lt;br /&gt;&lt;br /&gt;main = do&lt;br /&gt;    let answer = newton (func) (func&#39;) 1 4&lt;br /&gt;    print answer&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;The second method is called the &lt;span style=&quot;font-weight:bold;&quot;&gt;Secant method&lt;/span&gt;. Instead of using a tangent it uses a secant line, which is a line intersecting two points on the function curve. In the next iteration the next secant line will go through the last two x values. In this method like in the Bisection Method we need to choose the two initial guesses around the true solution! In other words for the two initial guesses x0, x1 the following must be true f(x0)*f(x1)&amp;lt;0 to guarantee a solution for a continues function.&lt;/p&gt;&lt;p&gt;The formula is x = x1 - f(x1)(x1-x0)/(f(x1)-f(x0)).&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;&lt;br /&gt;type F = Float&lt;br /&gt;secant :: (F -&gt; F) -&gt; F -&gt; F -&gt; Integer -&gt; [F]&lt;br /&gt;secant _ _ _ 0 = []&lt;br /&gt;secant f x0 x1 k = xc : secant f x1 xc (k-1)&lt;br /&gt;    where xc = x1 - f(x1)*(x1-x0)/(f(x1)-f(x0))&lt;br /&gt;&lt;br /&gt;func :: F -&gt; F&lt;br /&gt;func x = x^3+x-1&lt;br /&gt;&lt;br /&gt;main = do&lt;br /&gt;    let answer = secant (func) 0.5 1 4&lt;br /&gt;    print answer&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/2275032227770315047/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2010/01/newton-secant-methods.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/2275032227770315047'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/2275032227770315047'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2010/01/newton-secant-methods.html' title='Newton &amp; Secant Methods'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-6556610781754801532</id><published>2010-01-09T16:56:00.001+01:00</published><updated>2010-01-09T16:57:02.452+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BisectionMethod"/><category scheme="http://www.blogger.com/atom/ns#" term="Fixed-Point-Iteration"/><category scheme="http://www.blogger.com/atom/ns#" term="HaskNum"/><title type='text'>Bisection Method vs Fixed-Point Iteration</title><content type='html'>&lt;p&gt;How much faster is FPI compared to the Bisection Method? For that we need to think about accuracy. Or the error compared to the true solution. Now I know we don&#39;t know the true solution otherwise we would not be needing these numeric methods! But we can enclose the true solution inside an interval where we know that it lies in it (well for a continuous functions in the interval of interest).&lt;/p&gt;&lt;p&gt;Starting from The Bisection Method we know for each step the interval [a,b] is cut in half! After n steps our new interval [an, bn] have the length (b-a)/2^n. But choosing the solution to be the midpoint of the n-interval we know the true solution (if it is not the midpoint) is within an interval of length (b-a)/2^(n+1).&lt;/p&gt;&lt;p&gt;A solution is &lt;b&gt;correct within &lt;/b&gt;p&lt;b&gt; decimal places&lt;/b&gt; if the error |xc-r|&amp;lt;0.5*10^-p.&lt;/p&gt;&lt;p&gt;How many times do we need to cut the interval [0,1] in half to get an approximation with 6 correct digits for the problem f(x)=x^3+x-1 using the Bisection Method?&lt;/p&gt;&lt;p&gt;Solving (b-a)/2^(n+1) &amp;lt; 0.5*10^(-6) =&amp;gt; n = 19.9. Which means The Bisection method will need 20 steps for 6 correct digits. The approximated solution with 6 correct digits is xc=0.682327.&lt;/p&gt;&lt;p&gt;Using FPI with g(x)=(1+2x^3)/(1+3x^2) and an initial guess x0=1. After 4 steps we get the same solution. A major improvement of number of steps by using FPI with a &quot;nice&quot; g(x)!&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/6556610781754801532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2010/01/bisection-method-vs-fixed-point.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/6556610781754801532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/6556610781754801532'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2010/01/bisection-method-vs-fixed-point.html' title='Bisection Method vs Fixed-Point Iteration'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-128861897894675942</id><published>2010-01-09T16:54:00.001+01:00</published><updated>2010-01-10T11:56:47.025+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Fixed-Point-Iteration"/><category scheme="http://www.blogger.com/atom/ns#" term="HaskNum"/><title type='text'>Fixed-Point Iteration</title><content type='html'>&lt;p&gt;&lt;b&gt;Fixed-Point Iteration&lt;/b&gt;, FPI for short, is another equation solving method. But! Unlike The Bisection Method you have to exercise more caution.&lt;/p&gt;&lt;p&gt;First we need to rewrite the initial problem from f(x)=0 to g(x)=x and iterate until the solution stabilizes. Here is the code for FPI:&lt;pre class=&quot;codeview&quot;&gt;fpi :: (F -&gt; F) -&gt; F -&gt; Integer -&gt; [F]&lt;br /&gt;fpi _ _ 0 = []&lt;br /&gt;fpi g x0 k = xc : fpi g xc (k-1)&lt;br /&gt; where xc = g(x0)&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;The code is simple and the method returns a list with the intermediate approximated solutions. The best solution is in the end of the list.&lt;/p&gt;&lt;p&gt;Let us try to solve the same equation as the one presented in The Bisection Method, f(x)=x^3+x-1. Re-writing it we would get the equation: x=1-x^3 thus g(x)=1-x^3 (remember it should be of the form g(x)=x). But FPI will fail and the list returned would look like:&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;[0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,...]&lt;/pre&gt;&lt;p&gt;That is no good! But if we happen to add 2x^3 to both sides of the equation, and rearrange things we get: x = (1+2x^3)/(1+3x^2). So using g(x)=(1+2x^3)/(1+3x^2) we will have a good approximation in just 6 steps!&lt;/p&gt;&lt;pre class=&quot;codeview&quot;&gt;[0.75,0.68604654,0.68233955,0.68232775,0.6823278,&lt;br /&gt;0.6823278]&lt;/pre&gt;&lt;p&gt;Why? Well because there is a theorem that says: Assume g(x) is continuously differentiable, and g(r)=r, and |g&#39;(r)|&amp;lt;1. Then FPI converges to the fixed point r for sufficiently close initial guess to r.&lt;/p&gt;&lt;p&gt;Checking |g&#39;(r)| (r≃0.68) for the first attempt we can see that it is larger than 1, but not the second attempt.&lt;/p&gt;&lt;p&gt;FPI can converge to a solution faster than The Bisection Method, but we need to re-write the original solution. Because we dont now the solution r beforehand we can test re-writing it differently until FPI succeeds.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/128861897894675942/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2010/01/fixed-point-iteration.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/128861897894675942'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/128861897894675942'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2010/01/fixed-point-iteration.html' title='Fixed-Point Iteration'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8653762156638787988.post-3661062089726043171</id><published>2010-01-09T16:48:00.000+01:00</published><updated>2010-01-10T13:46:13.366+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BisectionMethod"/><category scheme="http://www.blogger.com/atom/ns#" term="HaskNum"/><title type='text'>Bisection Method</title><content type='html'>&lt;p&gt;Hi fellow internet user,&lt;/p&gt; &lt;p&gt;I am a computer engineering student in my last year (not quite true but its a long story!). I have recently learned &lt;b&gt;Haskell&lt;/b&gt; which I quite fancy for the moment. I was wondering how I can find the motivation to dwell into this particulate programming language more than just passing my course assignments! Fortunate or not I have not passed my &lt;b&gt;Numerical Analysis&lt;/b&gt; course. So while waiting for the re-exam in late August I will rewrite some of the numerical methods presented to me in the course in Haskell instead of as Matlab scripts.&lt;/p&gt; &lt;p&gt;And I start with an easy one, a bracketing method called &lt;b&gt;The Bisection Method. &lt;/b&gt;Without further ado here is my code:&lt;pre class=&quot;codeview&quot;&gt;type F = Float&lt;br /&gt;&lt;br /&gt;bisec :: (F -&gt; F) -&gt; (F,F) -&gt; F -&gt; F&lt;br /&gt;bisec f (a, b) tol&lt;br /&gt;    | (b-a)/2 &lt; tol    = (a+b)/2&lt;br /&gt;    | f(c) == 0        = c&lt;br /&gt;    | f(a)*f(c) &lt; 0    = bisec f (a,c) tol&lt;br /&gt;    | otherwise        = bisec f (c,b) tol&lt;br /&gt;               where c = (a+b)/2&lt;br /&gt;&lt;br /&gt;func :: F -&gt; F&lt;br /&gt;func x = x^3+x-1&lt;br /&gt;&lt;br /&gt;main = do&lt;br /&gt;    let answer = bisec (func) (0, 2) 0.0000005&lt;br /&gt;    print answer&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://monthadar.blogspot.com/feeds/3661062089726043171/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://monthadar.blogspot.com/2010/01/hi-fellow-internet-user-i-am-computer.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/3661062089726043171'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8653762156638787988/posts/default/3661062089726043171'/><link rel='alternate' type='text/html' href='http://monthadar.blogspot.com/2010/01/hi-fellow-internet-user-i-am-computer.html' title='Bisection Method'/><author><name>Monthadar</name><uri>http://www.blogger.com/profile/18149415436556195788</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>