MATLAB- for loop saving results of 3 variables

In summary, the conversation discusses using an iteration method in MATLAB to solve for 3 variables in 3 non-linear equations and how to track the changes of these variables when a parameter is changed. It also mentions the desire to save the results in a 3*5 matrix and asks for help with the problem.
  • #1
Economist08
5
0
MATLAB- for loop and interation

Hi
I'm using an interation method to solve for 3 variables in 3 non-linear equations. I would like to find out, how the 3 variables change, when I change a parameter in the 3 equations. Let's assume the parameter runs from 1 to 5 (1:1:5).

The iteration looks like this:
x0 = [1; 1; 1]; % Make a starting guess at the solution
options=optimset('Display','iter'); % Option to display output
[x,fval] = fsolve('file_that defines_the_3_functions',x0,options) % Call optimizer, where x is vector of the 3 variables

Preferable I would like to save the results in a 3*5 matrix, where n is the number variables and 5 the number of different parameters.
Please help!

Thanks in advance.
 
Last edited:
Physics news on Phys.org
  • #2
In the last sentence, I meant "where 3 is the number of variables...", not n.
 
  • #3
Is this question not clear? Please let me know, when I need to clarify the question. :smile:
 
  • #4
I have the same problem!
I know this was posted 3 years ago, so if you managed to solve the problem can you please share the solution?
Thank you!
 
  • #5


Hello,

Thank you for sharing your MATLAB code and question. It seems like you are trying to use a for loop to iterate through different values of a parameter and solve for the 3 variables in each iteration. This is a common approach in scientific computing and can be easily implemented in MATLAB.

To save the results in a 3*5 matrix, you can create an empty matrix before the for loop using the command "results = zeros(3,5)". Then, in each iteration, you can save the results in the corresponding column using the command "results(:,i) = x". This will save the values of the 3 variables in the i-th column of the results matrix.

Your code might look something like this:

x0 = [1; 1; 1]; % Make a starting guess at the solution
options=optimset('Display','iter'); % Option to display output
results = zeros(3,5); % Create empty matrix to store results
for i = 1:5 % Loop through different parameter values
[x,fval] = fsolve('file_that defines_the_3_functions',x0,options) % Call optimizer, where x is vector of the 3 variables
results(:,i) = x; % Save results in corresponding column
end

I hope this helps and good luck with your research!
 

1. What is a for loop in MATLAB?

A for loop is a programming construct in MATLAB that allows you to repeatedly execute a block of code for a specified number of times. It is often used for repetitive tasks, such as iterating through a set of data or performing calculations on multiple variables.

2. How do I save results of a for loop in MATLAB?

To save the results of a for loop in MATLAB, you can use the "save" function. This function allows you to save the variables you want to keep into a .mat file, which can be loaded and accessed later on.

3. Can I save results from multiple variables in a for loop?

Yes, you can save results from multiple variables in a for loop by using the "save" function with the appropriate syntax. You can specify the names of the variables you want to save, and they will be stored in the .mat file as separate variables.

4. How can I access the saved results from a for loop in MATLAB?

To access the saved results from a for loop in MATLAB, you can use the "load" function. This function allows you to load the .mat file containing the saved variables and assign them to new variables, which can then be used in your code.

5. Is it necessary to save results from a for loop in MATLAB?

No, it is not always necessary to save results from a for loop in MATLAB. If you only need the results for the current session, you can simply display them on the screen. However, if you want to use the results for future analysis or share them with others, it is recommended to save them for later use.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
820
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
989
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top