Solving Function Problem in Code Exchange

In summary, your code is poorly written and does not follow best practices for variable naming. You should try to follow more established naming conventions and use better names for your variables. Your code also has several errors, most notably passing j as a function parameter.
  • #1
Galizius
14
0
Hello I am trying to exchange following code into two functions which I will need to use in further programs.
The code is as follows:

Code:
do i=0,k
  do j=0,k
  if (i/=j) then
    lj(i)=lj(i)*((xx-x(i))/(x(j)-x(i)))
  end if
  enddo
  l(i)=l(i)+y(i)*lj(i)
  write(2,*) x(i),y(i),l(i)
enddo

I would like to swap lj and l arrays for the functions. I have tried something like that:

Code:
real(8) function lj(j,xx,x,k)
  real(8) :: x(1000)
  real(8) :: xx
  integer :: i,j,k

  lj=1.d0
do i=0,k
  do j=0,k
  if (i/=j) then
    lj=lj*((xx-x(i))/(x(j)-x(i)))
  end if
  enddo
enddo

Code:
real(8) function l(xx,x,y,k)
real(8) :: x(1000),y(1000), xx
integer :: i,j,k
  do i=0,k
    l=l+y(i)*lj(i,xx,x,k)
  enddo

And then I am trying to call it in the main program
Code:
do i=0,k
  write(2,*) x(i),y(i),l(x(i),x,y,k)
enddo

But it does not seem to be working as before. What am I doing wrong?
 
Last edited:
Technology news on Phys.org
  • #2
Galizius said:
But it does not seem to be working as before.
This is very vague. You'll get better help when you clearly describe why (you think) it is wrong.

Galizius said:
Code:
real(8) function l(xx,x,y,k)
real(8) :: x(1000),y(1000), xx
integer :: i,j,k
  do i=0,k
    l=l+y(i)*lj(i,xx,x,k)
  enddo
l is not initialized before being used in the loop.
 
  • #3
Galizius said:
Hello I am trying to exchange following code into two functions which I will need to use in further programs.
What am I doing wrong?

Not sure. In my compiler arrays are 1-based and dividing by 0 is disastrous. From your snippets I can come to something that runs but I have no idea if it's what you intend:
Code:
real(8) function lj(j,xx,x,k)
  real(8) :: x(1000)
  real(8) :: xx
  integer :: i,j,k
 
  lj=1.d0
 do i=1,k
  do j=1,k
  if (x(i) /= x(j)) then
  if (i/=j) then
  lj=lj*((xx-x(i))/(x(j)-x(i)))
  end if
  end if
  enddo
 enddo
end
 real(8) function l(xx,x,y,k)
 real(8) :: x(*),y(*), xx,  lj
 external lj
 integer :: i,j,k
  do i=1,k
  l=l+y(i)*lj(i,xx,x,k)
  enddo
 end
 
Program test
real*8 l,x(1000),y(1000)
external l
k=4
do i=1,k
  write(2,*) x(i),y(i),l(x(i),x,y,k)
pause
enddo
end

with output

Code:
  0.000000000000000E+000  0.000000000000000E+000  6.790386531088871E-313

suspicious ! We should probably initialize a few things
 
  • #4
I just noticed another problem:

Galizius said:
Code:
real(8) function lj(j,xx,x,k)
  real(8) :: x(1000)
  real(8) :: xx
  integer :: i,j,k

  lj=1.d0
do i=0,k
  do j=0,k
  if (i/=j) then
    lj=lj*((xx-x(i))/(x(j)-x(i)))
  end if
  enddo
enddo

Code:
real(8) function l(xx,x,y,k)
real(8) :: x(1000),y(1000), xx
integer :: i,j,k
  do i=0,k
    l=l+y(i)*lj(i,xx,x,k)
  enddo
The code is accessing x(0) and y(0), which are outside the array boundaries.
 
  • Like
Likes Galizius
  • #5
BvU said:
Not sure. In my compiler arrays are 1-based and dividing by 0 is disastrous.
I guess you didn't mean dividing by 0, in which case you beat me to it!
 
  • #6
DrClaude said:
I just noticed another problem:The code is accessing x(0) and y(0), which are outside the array boundaries.

That solved my problem! Thank You so much, I do not know how could I miss that...
 
  • #7
Galizius said:
That solved my problem! Thank You so much, I do not know how could I miss that...
Be sure to also implement the fix to the problem I mentioned in post #2. It might work for now, but you never know when it might fail.
 
  • Like
Likes Galizius
  • #8
Yes, I already did. Thank You again!
 
  • #9
Galizius said:
Fortran:
real(8) function lj(j,xx,x,k)
  real(8) :: x(1000)
  real(8) :: xx
  integer :: i,j,k
  lj=1.d0
do i=0,k
  do j=0,k
  if (i/=j) then
    lj=lj*((xx-x(i))/(x(j)-x(i)))
  end if
  enddo
enddo
A few comments about your code:
  1. The names you have picked for most of your variables are terrible! Single letter names such as i and j are OK for loop control variables, but names like lj, x, xx, and k don't give any indication of what you're going to use them for. There is no reason not to choose a name that suggest what the variable represents. Many years ago there were limits on how long a name you could use for a variable, so having variable names like x, xx, and lj is completely unwarranted, and makes your code harder to read than it should be.
  2. Why are you passing j as a parameter of your function? This variable gets set to 0, 1, 2, ..., k. It is strictly a local loop control variable, so there is no point in passing it as a function parameter.
  3. There isn't a single comment in your code.
  4. There isn't a single whitespace character in your code. The compiler doesn't care, but it's much harder for humans to read this -- lj=lj*((xx-x(i))/(x(j)-x(i))) -- than this --
    lj = lj * ( (xx - x(i) ) / ( x(j) - x(i) ) )
 

1. What is a function in code exchange?

A function in code exchange is a reusable block of code that performs a specific task. It helps to organize code and makes it easier to maintain and debug.

2. How do you solve a function problem in code exchange?

To solve a function problem in code exchange, you need to first understand the problem and what the expected output should be. Then, you can write a function using the appropriate syntax and logic to solve the problem. Testing and debugging the function is also important to ensure it is functioning correctly.

3. What is the purpose of solving function problems in code exchange?

The purpose of solving function problems in code exchange is to create efficient and reusable code. Functions help to reduce code repetition and make it easier to maintain and modify code in the future.

4. How do you know if a function problem has been solved correctly in code exchange?

You can test the function with different inputs and compare the output to the expected result. If the function consistently produces the correct output, then the problem has been solved correctly.

5. Are there any tips for solving function problems in code exchange?

Some tips for solving function problems in code exchange include breaking the problem down into smaller parts, using descriptive function names and comments, and testing and debugging the function thoroughly. It is also helpful to use online resources and consult with other programmers if needed.

Similar threads

  • Programming and Computer Science
Replies
4
Views
590
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
1
Views
935
  • Programming and Computer Science
Replies
12
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top