How to automatically add/subtract lines of code into a matlab function for several iterations?

So, I have this topology optimization function (models a beam problem with supports/loads on it).
It takes 5 input variables and generates an image of a certain topology.
I know how to randomly generate different #s for the 5 input variables to create different topologies.
But, there's some lines in the function code that work to apply one load at the end of the beam.
I want to be able to automatically change this part of the code so that the code will randomly choose between 1-2 applied loads and each at different random locations. This way, I can generate different topologies based off how many/where loads are applied
How would I go about this?
I was given the following advice, but don't really understand what was meant by this/ how to implement input files:
  • Automate the code, set up input files to iterate through, different input file for loads at different locations or volume constraints, etc.
  • Change the code so that instead of all values being embedded within code, everything u want to get changed is through input file, automate thru and rewrite input file over and over again

댓글 수: 6

"I want to be able to automatically change this part of the code ... How would I go about this?"
There be dragons!
I strongly recommend that you avoid meta-programming (that is: code that (re-)writes code). A much better approach is to rewrite the code yourself so that your function accepts input parameters, and then simply call the function with different parameters. Then you just need to call the function in a loop, and on each iteration provide it with the next set of parameters, including random locations, etc.
You could use files to provide those parameters, or some kind of array (e.g. numeric, cell, table, struct).
Okay, so i've done the following:
I made 2 separate functions: the 1st function has coe that applies 1 load to the end of the beam.
the 2nd function has code that applies 2 loads at the end of the beam.
So, now in my main script, I wrote the following:
loadnum=randi([1 2], 1,1);
if loadnum==1
topload1(nelx,nely,volfrac,penal,rmin)
if loadnum==2
topload2(nelx,nely,volfrac,penal,rmin)
This allows me to choose between 1 or 2 loads. HOWEVER....I'm not sure how I would go about randomly assigning locations for the load for each of those functions.
Would I include lines of code within each of these functions that will randomly choose a location for the load?
"Would I include lines of code within each of these functions that will randomly choose a location for the load?"
Generate the random locations (and any other parameters) in the calling script (e.g. within the loop). The functions should just perform calculations on the provided data.
As an aside: is it really required to have two functions? Why not just one with an input to specify the locations of the loads?
because the lines of code for having 1 load are different than the lines of code for having 2 loads (so i'd basically have to delete the lines of code for load 1 and substitute lines of code for load 2).
It's not just a variable input like "loads=2". In fact, in order to change it from 1 load to 2 loads, there are about 3 sections within the function code that I have to rewrite.
The same thing for the locations- it's not just (x,y) points (depends on the topology generated).
@Rachel Dawn: fair enough, thank you for the explanation. Good luck with your task!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2020년 10월 20일

편집:

2020년 10월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by