Question

write a c code for df/dx=x^2 solution with a boundary condition f(1)= 2 and h= 0.01

write a c code for df/dx=x^2 solution with a boundary condition f(1)= 2 and h= 0.01

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//We are going to use Euler's method of solving differential equation
//Here, y_next = y_prev + h*function(x_prev, y_prev)
//we have f(x) = x^2

#include<stdio.h>
double function(double);
int main() {
        int i = 0;
        double x_prev, x_next;
        double y_next, y_prev, h;
        h = 0.01;                                       //step size
        x_prev = 1;                                     //initial value of x
        y_prev = 2;                                     //boundary value
        do {
                y_next = y_prev + h * function(x_prev);
                x_next = x_prev + h;
                printf("x\t\ty\t\th\n");
                printf("%4.6lf\t%4.6lf\t%4.6lf\n", x_next, y_next, h);
                x_prev = x_next;
                y_prev = y_next; 
                i++;
        } while (i<20);                      //You can also use y_next-y_prev <0.001 like condition
        getchar();
        return 0;
}
double function(double x) {
        return x*x;
}

Add a comment
Know the answer?
Add Answer to:
write a c code for df/dx=x^2 solution with a boundary condition f(1)= 2 and h= 0.01
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT