Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- Help for iteration

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-11-2018
01:42 PM

12-11-2018
01:42 PM

Hey guys,

I'm new in Mathcad and I'm doing my thesis now. I have some trouble finding how to do iteration in Mathcad.

So in my file, I have a reactor of 250 m. And I want to know how the concentration changes.

But, I want the simulation to run for >1 cycle. Which means I have to define a way to make the Cout (concentration out) from previous cycle, as a value for the Cin(2) concentration in of the next cycle. These are for both liquid and gas concentration.

So in summary I want to make a loop of :

COlin (x+1) = COlout (x)

COgin (x+1) = COgout (x) + COgin (x)

Help guys 😞

Thank you in advance

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
10:50 AM

12-13-2018
10:50 AM

@NandaV wrote:

I mean now I have the for loop cycle after the 1st cycle derivation.

No. I just left an example of a single cycle so you could see that changing the Odesolve block to a function didn't alter the calculation. In the attached I've removed this!

@NandaV wrote:

Also, isn't now the system taking the value from previous x point, so the COlin (x) = COlout (X-1) instead of per cycle?

No. Each cycle covers all values of x before updating the inlet values for the next cycle. This is because all values of x are considered by the Odesolve function which is called just once per cycle.

Alan

28 REPLIES 28

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-11-2018
03:11 PM

12-11-2018
03:11 PM

Re: Help for iteration

What you have is two coupled first order differential equations. Your "Given" block is missing its closure.

Given needs to be a closed block, in your case with Odesolve. Review help on solving differential equations--there should be an example that is directly applicable.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
03:45 AM

12-12-2018
03:45 AM

Re: Help for iteration

Hey Fred,

Thanks for your reply. What do you mean by closed solve block?

Because in that file i'm using the "Given" which means I have the solve block already?

Now i'm just confused in trying to figure out how to loop the result as an input of the next derivation.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
10:10 AM

12-12-2018
10:10 AM

Re: Help for iteration

@SavitriN wrote:

Hey Fred,

Thanks for your reply. What do you mean by closed solve block?

Because in that file i'm using the "Given" which means I have the solve block already?

Now i'm just confused in trying to figure out how to loop the result as an input of the next derivation.

A solve block opens with "Given".

It closes with "Find" or with "Minerr" or with "Minimize", or with :Maximize," or with "ODEsolve" (maybe I've missed a couple.)

Yours doesn't close.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
11:30 AM

12-12-2018
11:30 AM

Re: Help for iteration

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
11:31 AM

12-12-2018
11:31 AM

Re: Help for iteration

Yes I did closed it with the Odesolver…

As you can see in my attached picture.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
02:04 PM

12-12-2018
02:04 PM

Re: Help for iteration

So you should have two functions of the two parameters. What happens if you type C.O2lSS(2m), or try to plot C.02lSS(x) vs x?

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
05:16 PM

12-12-2018
05:16 PM

Re: Help for iteration

Fred, in case you can't open and view the OPs worksheet, here is a pdf print of it.

He had already solved the ODE system and I have no clue which iteration he is asking for.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
03:43 AM

12-13-2018
03:43 AM

Re: Help for iteration

Hey Werner & Fred, What i meant by iteration is a for loop.

I have a system with a fix inlet and outlet which has a length of 250 m.

1 cycle means that the system run from inlet into outlet for 1 time.

When i run the derivation, i use intial value of Clin (concentration liquid in) and Cgin (concentration gas in) which will give a result of Clout (concentration liquid out) and Cgout (concentration gas out).

For the 2nd cycle, i want to use Clout for the initial value instead of Clin. And for the gas, i will always want to use Cgin as intial value (fresh air).

Clin1cycle = Clin & Clin2cycle = Clout1cycle & Clin3cycle = Clout2cycle, etc.

Cgin1cycle = Cgin2cycle = Cgin2cycle = Cgin

However, i dont know how to translate in into for loop command in mathcad.

That's what i was trying to ask.

Thank you

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
04:20 AM

12-13-2018
04:20 AM

Re: Help for iteration

Is the attached what you are looking for?

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
04:20 AM

12-13-2018
04:20 AM

Re: Help for iteration

Is the attached what you are looking for?

Alan

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
04:47 AM

12-13-2018
04:47 AM

Re: Help for iteration

Hey Alan,

I think yes but i do not understand the outliq (L.total) and outgas(L.total) term and do i have to add inside the ODE solver the determination of cycles ?

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
06:55 AM

12-13-2018
06:55 AM

Re: Help for iteration

@NandaV wrote:

Hey Alan,

I think yes but i do not understand the outliq (L.total) and outgas(L.total) term and do i have to add inside the ODE solver the determination of cycles ?

The number of cycles is set by the variable ncycles that is assigned immediately before the program structure. I've arbitrarily set ncycles to 5. You will need to set it to whatever is appropriate for your problem.

I've assumed that for given inlet values of gas and liquid, the calculations are done for every section (x) from 1 to Ltotal. Within the program I've used dummy variables outliq and outgas to calculate the values of CO2l and CO2g for every section (x) (via the Odesolve block, which is now a function of the inlet values). The inlets for the next cycle, inliq and ingas are then set to the outlet values of outliq and outgas for the previous cycle, hence they are set to the last section values, namely outliq(Ltotal) and inliq(Ltotal). The final values of outliq and outgas each a vector of length Ltotal, are then returned to CO2lSS and CO2gSS respectively.

Alan

Edit: My description above is not completely correct! outliq and outgas are not vectors, but functions (of x), so that CO2lSS and CO2gSS are also functions.

Alan

(For some reason my previous reply was posted twice!!)

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
10:38 AM

12-13-2018
10:38 AM

Re: Help for iteration

Ah.. okay I understand. But is it possible to do the derivation only once?

I mean now I have the for loop cycle after the 1st cycle derivation.

Also, isn't now the system taking the value from previous x point, so the COlin (x) = COlout (X-1) instead of per cycle? (I hope you understand what I meant).

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
10:50 AM

12-13-2018
10:50 AM

@NandaV wrote:

I mean now I have the for loop cycle after the 1st cycle derivation.

No. I just left an example of a single cycle so you could see that changing the Odesolve block to a function didn't alter the calculation. In the attached I've removed this!

@NandaV wrote:

Also, isn't now the system taking the value from previous x point, so the COlin (x) = COlout (X-1) instead of per cycle?

No. Each cycle covers all values of x before updating the inlet values for the next cycle. This is because all values of x are considered by the Odesolve function which is called just once per cycle.

Alan

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-14-2018
04:40 AM

12-14-2018
04:40 AM

Re: Help for iteration

Yes. I understand now. Thank you so much Alan!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-17-2018
05:04 AM

12-17-2018
05:04 AM

Re: Help for iteration

Hey Alex,

So, there is another approach i should look into. Instead of coupling the CO2lSS & CO2gSS in one derivation.

I have to first solve the CO2lSS with ode solver and resulted in CO2lSS(x).

And from there i substitute into the equation of CO2gSS(x) equation, where inside the equation there the CO2gin and CO2lin which has to equal to the CO2lout (x-1) and CO2gout (x-1).

So i have to make the new For loop, i tried but it still gives error. Do you mind helping me with this?

I would really appreciate it.

Thank you.

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-17-2018
10:54 AM

12-17-2018
10:54 AM

Re: Help for iteration

Something like the attached perhaps?

It would help if you were (1) to specify the units of *all* the constants and variables, and (2) to show a system diagram. I would then be in a much better position to check my interpretation of what I think you are after!

Alan

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-17-2018
11:42 AM

12-17-2018
11:42 AM

Re: Help for iteration

Thanks for the reply.

So, the re-circulation that i needed to simulate is the one presented above. The concentration in liquid is solved first by the ODE solver and results in CO2l(x) to simulate each concentration in every x point. Then, the CO2g(x) can be found by substitution.

I'm not sure should i do the for loop for gas and liquid in 1 command, or should i do it separately (like you did in previous file). I hope it makes thing clearer...

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-17-2018
11:42 AM

12-17-2018
11:42 AM

Re: Help for iteration

Notice that your liquid phase equations have an analytical solution, so, alternatively, you could do the calculations as in the attached

Alan

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-17-2018
02:48 PM

12-17-2018
02:48 PM

Re: Help for iteration

Okay, i will try that. Thank you very much!!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-28-2018
04:37 PM

12-28-2018
04:37 PM

Re: Help for iteration

Hey Alan,

Looking at the result curve, i'm having trouble now in plotting the result.

I can only plot the first cycle and the last cycle.

Which means if I set n>2 cycles, I can not see and plot the curve for the middle between cycles 2 and last cycle.

Do you mind helping me again to solve this problem?

Thanks

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-29-2018
04:17 AM

12-29-2018
04:17 AM

Re: Help for iteration

See the attached.

Alan

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-30-2018
05:44 AM

12-30-2018
05:44 AM

Re: Help for iteration

Dear Alan,

Thank you very much!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

01-11-2019
12:34 PM

01-11-2019
12:34 PM

Re: Help for iteration

Hey Alan,

It's me again haha. So I was wondering, from previous iteration it is correct that the value from previous cycle got taken up as an input value for the next cycle. However, I noticed that actually the liquid and gas derivation is not connected, while it's supposed to be connected. So, the liquid derivation has to take the new COgin as well, and same for the gas substitution has to take new COlin. While now, the new COgin value only used for gas derivation and the new COlin value only used for liquid derivation.

I made the new iteration as attached, do you think it's correct?

Thanks

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-13-2018
09:15 AM

12-13-2018
09:15 AM

Re: Help for iteration

@Werner_E wrote:

Fred, in case you can't open and view the OPs worksheet, here is a pdf print of it.

He had already solved the ODE system and I have no clue which iteration he is asking for.

Thanks Werner,

Smath will open parts of xmcd drawings (not Prime), but it just skips what it doesn't understand (things like "Find" and graphs. So I can speak to some of the problems, but not what I can't see. . .

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-11-2018
04:11 PM

12-11-2018
04:11 PM

Re: Help for iteration

Maybe you should look here.

https://community.ptc.com/t5/PTC-Mathcad/Integration-Solver/m-p/578494#M183033

It deals with (what appears to be/almost?) the exact same problem.

Success!

Luc

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
03:42 AM

12-12-2018
03:42 AM

Re: Help for iteration

Hey Luc, unfortunately, that is my other account which I forgot the password..

So that is still my case haha :'')

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-12-2018
10:07 AM

12-12-2018
10:07 AM

Re: Help for iteration

There's an old method in Mathcad that mimics Euler's approximation to ODE's. This might work for you.

(set ORIGIN to 1)