help-octave
[Top][All Lists]

Re: A couple of beginner's questions

 From: Stefan Pofahl Subject: Re: A couple of beginner's questions Date: Wed, 27 Feb 2008 07:40:44 +0100

```Here is my answer, perhaps it helps to handle the function
"fminbnd" for octave beginners, the documentation for the

Regards,

Stefan

############################################################
## How would I command Octave in order to find all the values of x that would
## make the functions
##
## f1(x) = 1.035568^x * 1000 - 1000 and
## f2(x) = (1.025668^x * 1000 - 1000) * 0.72
##
## equal with each other?
## --
## My attempt to solve the problem: transform it into a optimization problem:
## y = (f1(x) - f2(x))^2, optimization: y should be zero
function numminen()
x=[-100:20]'; # vector with one column
y1= x;         # tell octave the format an size of the array y1
y2= x;         # tell octave the format an size of the array y2
for i=1:size(x)(1)
y1(i)=testfunction(x(i));
y2(i)=minimumfunction(x(i));
endfor

## fminbnd is part of octave-forge package
## and searches for the minimum of a function, between a and b:
## http://octave.sourceforge.net/doc/optimization.html

## first approach: a "anonymous function"
anonymtestf = @(x) (1.035568^x * 1000.0 - 1000.0 + ...
(1.025668^x * 1000.0 - 1000.0) * 0.72)^2;

[xatmin,ymin]=fminbnd(anonymtestf, -100, 20)

## second approach, use a named function, here you need the "@"
[xatmin,ymin]=fminbnd(@minimumfunction, -100, 20)

## plotting
clg;
hold on;
plot(x,y1,";testfunction;");
plot(x,y2/max(y1), ";minimumfunction;");
## legend("testfunction", "minimumfunction") #octave 3.0
hold off;

function [y] = testfunction(x)
y= (1.035568^x * 1000.0 - 1000.0 + (1.025668^x * 1000.0 - 1000.0) * 0.72);
endfunction # end testfunction()

function [y] = minimumfunction(x)
y= (1.035568^x * 1000.0 - 1000.0 + (1.025668^x * 1000.0 - 1000.0) * 0.72)^2;
endfunction # end minimumfunction()
endfunction # end numminen
###################################################

> p.numminen wrote:
>
>  >
>  > Question 1:
>  > How would I command Octave in order to find all the values of x that would
>  > make the functions
>  >
>  > f1(x) = 1.035568^x * 1000 - 1000 and
>  > f2(x) = (1.025668^x * 1000 - 1000) * 0.72
>  >
>  > equal with each other?
>  >
>
>
> If it is just a one time thing, you can just plot the functions using say
>  gnuplot, and zoom in on the point of intersection.
>
>
>  >
>  > Question 2:
>  > How would I command Octave in order to solve the equation
>  >
>  > 1.035568^x * 1000 - 1.025668^x * 720 - 280 = 0
>  >
>
>
> Same again. Use gnuplot or other plotting software and zoom in where the
>  curve hits x-axis.
>
>  raju
>
> --
>  Kamaraju S Kusumanchi
>  http://www.people.cornell.edu/pages/kk288/
>  http://malayamaarutham.blogspot.com/
>
>
>  _______________________________________________
>  Help-octave mailing list
>  https://www.cae.wisc.edu/mailman/listinfo/help-octave
>

--
Tel.: 0731-3805149
Ochsensteige 48
89075 Ulm

```