Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Goto in pascal to Matlab

Status
Not open for further replies.

Tina2796

Technical User
Jan 29, 2010
1
0
0
US
Does anyone know how to convert this program from pascal to matlab? The goto statement is really confusing me. I just don't understand how it flows with the goto statements in there. If going to matlab cannot be explained , if I could get an explanation of what exactly this program is doing that would be great. I would really appreciate it.

Thanks, Tina

Bhigh:= 1.5E+11;
Blow := 1.5E+2;
100:
B := (Bhigh + Blow)/2.0;
w[1] := Ts; {Surface Temperature}
w[2] := (Ts-T0)/(d1*d2) + L; {Surface Temperature Slope}
y := 0.0;
repeat
If w[1] > 1.0 then begin
Blow := B;
goto 100;
end;
If w[2] < 0.0 then begin
Bhigh := B;
goto 100;
end;

Runge( Equations0, 2, 0.01, y, w );
{writeln(B:25:20,y:10:5);}
until Bhigh-Blow < 0.00000005;
 
I really don't know Matlab, but I might be able to help you understand this. The goto statement says in the code to go to the point of the specified label. When it says "goto 100", it is saying to "go to the point in the code where the label named 100 appears".

Upon web search, my best guess is that Matlab doesn't support goto. As far as adapting it goes, you would probably need to supply a little more about what this code is supposed to do, along with acceptable & expected data, so any suggestion we might give you on a gotoless code could be tested.

(and yes, goto is one of the things I tend to loathe, especially in this kind of case)

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
You need to rewrite it without a goto first, then translate.
First declare a boolean variable, say goto100

The code will look something like
Code:
Bhigh:= 1.5E+11;
Blow := 1.5E+2;
repeat
    B := (Bhigh + Blow)/2.0;
    w[1] := Ts;  {Surface Temperature}
    w[2] := (Ts-T0)/(d1*d2) + L; {Surface Temperature Slope}  
    y := 0.0;
    goto100 := false;
    repeat
        If w[1] > 1.0 then begin
            Blow := B;
            goto100 := true;
        end
        else If w[2] < 0.0 then begin
            Bhigh := B;
            goto100 := true;
        end
        else begin
           Runge( Equations0, 2, 0.01, y, w );
           {writeln(B:25:20,y:10:5);}
        end;
    until goto100 or Bhigh-Blow < 0.00000005;
until not goto100;
Could you translate that to Matlab? Like Glen999, I don't know Matlab either.
 
me said:
As far as adapting it goes, you would probably need to supply a little more about what this code is supposed to do, along with acceptable & expected data

I realized in reading this that it might sound daunting to you. What I meant was to maybe supply the full Pascal program, along with some idea of what the program does (i.e. calculates the Pygathorean theorem for input). I'm sure you know what it should do (and maybe have the EXE of this somewhere even), since you are wanting to do something with it in Matlab.

Most of us are Pascal-only people but can definitely help you, at least in terms of expressing the algorithm in constructs that Matlab allows. But we would need to be able to fully understand it in order to do that.

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
IMHO
Code:
Runge( Equations0, 2, 0.01, y, w );
could be for finding of solution w of initial value problem for system of 2 differential equations with right hand side y, integration step 0.01 and with the initial values
Code:
w[1] := Ts;  {Surface Temperature}
w[2] := (Ts-T0)/(d1*d2) + L; {Surface Temperature Slope}
 
I don't understand the purpose of the program, but IMHO your original code with goto and repeat-until loop
Code:
Bhigh:= [COLOR=#ff00ff]1[/color].5E+[COLOR=#ff00ff]11[/color];
Blow := [COLOR=#ff00ff]1[/color].5E+[COLOR=#ff00ff]2[/color];
[COLOR=#ff00ff]100[/color]: 
B := (Bhigh + Blow)/[COLOR=#ff00ff]2.0[/color];
w[[COLOR=#ff00ff]1[/color]] := Ts;  [COLOR=#0000ff]{Surface Temperature}[/color]
w[[COLOR=#ff00ff]2[/color]] := (Ts-T0)/(d1*d2) + L; [COLOR=#0000ff]{Surface Temperature Slope}[/color]  
y := [COLOR=#ff00ff]0.0[/color];
[COLOR=#804040][b]repeat[/b][/color]
  [COLOR=#804040][b]If[/b][/color] w[[COLOR=#ff00ff]1[/color]] > [COLOR=#ff00ff]1.0[/color] [COLOR=#804040][b]then[/b][/color] [COLOR=#804040][b]begin[/b][/color]
    Blow := B;
    [COLOR=#804040][b]goto[/b][/color] [COLOR=#ff00ff]100[/color];
  [COLOR=#804040][b]end[/b][/color];
  [COLOR=#804040][b]If[/b][/color] w[[COLOR=#ff00ff]2[/color]] < [COLOR=#ff00ff]0.0[/color] [COLOR=#804040][b]then[/b][/color] [COLOR=#804040][b]begin[/b][/color]
    Bhigh := B;
    [COLOR=#804040][b]goto[/b][/color] [COLOR=#ff00ff]100[/color];
  [COLOR=#804040][b]end[/b][/color]; 
  Runge( Equations0, [COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]0.01[/color], y, w );
  [COLOR=#0000ff]{writeln(B:25:20,y:10:5);}[/color]
[COLOR=#804040][b]until[/b][/color] Bhigh-Blow < [COLOR=#ff00ff]0.00000005[/color];
could be approximately transformated into this code with only one while-loop (which could you then recode in Matlab)
Code:
Bhigh:= [COLOR=#ff00ff]1[/color].5E+[COLOR=#ff00ff]11[/color];
Blow := [COLOR=#ff00ff]1[/color].5E+[COLOR=#ff00ff]2[/color];
[COLOR=#0000ff]{* before the loop:}[/color]
[COLOR=#0000ff]{* reset values *}[/color]
B := (Bhigh + Blow)/[COLOR=#ff00ff]2.0[/color];
w[[COLOR=#ff00ff]1[/color]] := Ts;  [COLOR=#0000ff]{Surface Temperature}[/color]
w[[COLOR=#ff00ff]2[/color]] := (Ts-T0)/(d1*d2) + L; [COLOR=#0000ff]{Surface Temperature Slope}[/color]  
y := [COLOR=#ff00ff]0.0[/color];
[COLOR=#0000ff]{****************}[/color]
[COLOR=#804040][b]while[/b][/color] Bhigh-Blow >= [COLOR=#ff00ff]0.00000005[/color] [COLOR=#804040][b]begin[/b][/color]
  [COLOR=#804040][b]if[/b][/color] (w[[COLOR=#ff00ff]1[/color]] > [COLOR=#ff00ff]1.0[/color]) [COLOR=#804040][b]or[/b][/color] (w[[COLOR=#ff00ff]2[/color]] < [COLOR=#ff00ff]0.0[/color]) [COLOR=#804040][b]then[/b][/color] [COLOR=#804040][b]begin[/b][/color]
    [COLOR=#804040][b]If[/b][/color] w[[COLOR=#ff00ff]1[/color]] > [COLOR=#ff00ff]1.0[/color] [COLOR=#804040][b]then[/b][/color] Blow  := B; [COLOR=#0000ff]{adjust new interval to <B, Bhigh>}[/color]
    [COLOR=#804040][b]If[/b][/color] w[[COLOR=#ff00ff]2[/color]] < [COLOR=#ff00ff]0.0[/color] [COLOR=#804040][b]then[/b][/color] Bhigh := B; [COLOR=#0000ff]{adjust new to <Blow, B>}[/color]
    [COLOR=#0000ff]{* reset values *}[/color]
    B := (Bhigh + Blow)/[COLOR=#ff00ff]2.0[/color];
    w[[COLOR=#ff00ff]1[/color]] := Ts;  [COLOR=#0000ff]{Surface Temperature}[/color]
    w[[COLOR=#ff00ff]2[/color]] := (Ts-T0)/(d1*d2) + L; [COLOR=#0000ff]{Surface Temperature Slope}[/color]  
    y := [COLOR=#ff00ff]0.0[/color];
    [COLOR=#0000ff]{****************}[/color]
  [COLOR=#804040][b]end[/b][/color]; 
  Runge( Equations0, [COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]0.01[/color], y, w );
[COLOR=#804040][b]end[/b][/color] [COLOR=#0000ff]{while}[/color]
But note, that the repeat-until loop checks the condition at end, but the while-loop checks the condition at the beginning of the loop. So the repeat-loop in your original code will surely be executed minimal one time, but the while-loop in the transformed code will be executed minimal one time only if the condition Bhigh-Blow >= 0.00000005 is fullfilled before the first iteration - in your case it's true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top