# Looping and Saving Values into an Array for Forward Euler Method Using While Loop

8 views (last 30 days)
hardygirl989 on 7 Apr 2016
Answered: sam0037 on 11 Apr 2016
Hello,
I was wondering if you could help me with a question. I have to edit a MATLAB file ForwardEuler_while.m to add a couple of tasks (Refer to image). My code right now, I think I edited it to work in the case for only one delta_t. However, I want to make some array so that I can plot the Forward Euler solutions for the four different values of delta t and the exact solution on one plot. I also want to be able to get the L1 error for each of the delta_t's and plot them. Right now, the code only does one delta_t and so there is only one L1 error value. I can make a seperate program using the data by running this program over and over, but I want to make it all in one program. Can you help me?
Thank you.
What I tried doing: My idea was to try to make an array (initialized as zeroes(n_max,1)) and then make the for loop so that the unew becomes unew(i+1) for delta_t(i+1), but at the end, I run into this problem with storing a number into a matrix and an array as well for the t_soln(n) and u_soln(n) as well.

sam0037 on 11 Apr 2016
Hi,
This can be achieved by calling a function repeatedly to perform the same task but for different values. To illustrate this I have attached a sample code which tries to do the same thing. Execute the script EULER.M which repeatedly calls the function MYEULER.M for different delta_t.
Feel free to modify the code to make changes according to the requirement. I assume you are facing the difficulty while saving the solution array (u_soln and t_soln) since you are using an array to store the data whose sizes are different. The size of the solution depends on delta_t. If you want to save the solution data for each delta_t, you can use the cell array .