How can i put this recurring calculation into a for loop

3 views (last 30 days)
so far my code is as shown below
clc
clear all
k = 143; % thermal conductivity of copper, W/(m-K)
rho = 2.8; % density of copper, kg/m^3
specificHeat = 795; % specific heat of copper, J/(kg-K)
x=input('Enter the dimension of the x axis of the plate in meters: ');
y=input('Enter the dimension of the y axid of the plate in Meters: ');
f0=input('Enter a value for the Fourier Number: ');
while f0 <= 0 f0>0.25
f0=input('Enter a value for the Fourier Number: ');
end
h=input('Enter the grid spacing: ');
while h<0
h=input('Enter the grid spacing: ');
end
a=(x/h)
xx=a+1
b=(x/h)
yy=b+1
m = zeros(xx,yy) % grid spacing of the plate
m(1,:)=50 % initial boundary conditions at top of plate
m(yy,:)=10 % initial boundary condition at bottom of platec
n=40/b
I am trying to put in boundary conditions along the left and right hand side i have worked out that the equation is
m(yy-c,1)=10+n
m(yy-c,1)=10+2*n
m(yy-c,1)=10+3*n
how can i put this into a for loop where the boundary conditions will be put in for any size matrix?
  2 Comments
Tamir Suliman
Tamir Suliman on 2 Dec 2016
you will have explain what you trying to do how is h < 0 and f0 less than 0 and greater than 0.25
If some one answers your questions please click approve answer to give them credit for the effort they took to answer your questions.
Sarah Chappell-Smith
Sarah Chappell-Smith on 4 Dec 2016
Hi h has to be greater than zero so the user can not enter a negative grid spacing as only positive integers can be used.
with regards to f0 these conditions have been given to be that f0 must be greater than zero but less than or equal to 0.25.
That part of the code seems to run fine its the final line where; m(yy-c,1)=10+n
m(yy-c,1)=10+2*n
m(yy-c,1)=10+3*n
When i put this into my code wit works if i type it manually but i need to put it into a for loop as the user defines the size of the matrix and depending on the size of the matrix this determines how many times the calculation shown above needs to be done.

Sign in to comment.

Answers (1)

Sid Jhaveri
Sid Jhaveri on 6 Dec 2016
I am assuming that according to the user given input, you have to calculate the number of iterations(loops) your for loop need to do and you would like to know how you can do this. If my assumption is correct, you can do this by following the bellow given pseudo-code:
1) Get the user input and store it in a variable. Let's say this variable is "in".
2) Use "in" to calculate the number of iterations needed and store this in a new variable. Let's say this new variable is "n". (I believe you have come up with a formula to calculate this.)
3) Run the for loop of "n" number of times.
For more information on "for loops", refer the link given below:

Community Treasure Hunt

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

Start Hunting!