simpsons fix
This commit is contained in:
		
							
								
								
									
										16
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/main.zig
									
									
									
									
									
								
							@@ -25,6 +25,8 @@ pub fn main() !void {
 | 
				
			|||||||
        plotGraph();
 | 
					        plotGraph();
 | 
				
			||||||
        drawAxis();
 | 
					        drawAxis();
 | 
				
			||||||
        try markAxis(allocator);
 | 
					        try markAxis(allocator);
 | 
				
			||||||
 | 
					        const area = try simpsonsRule(-5, 5, 1000000);
 | 
				
			||||||
 | 
					        try displayAreaSize(area);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        rl.endDrawing();
 | 
					        rl.endDrawing();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -125,7 +127,7 @@ fn plotGraph() void {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// f(x)
 | 
					// f(x)
 | 
				
			||||||
fn getY(x: f32) !f32 {
 | 
					fn getY(x: f32) !f32 {
 | 
				
			||||||
    return try math.calculate(x, 2, .Power) / -2 + 6;
 | 
					    return try math.calculate(x, 2, .Power);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn adjustForGridSystem(input_vector: rl.Vector2) rl.Vector2 {
 | 
					fn adjustForGridSystem(input_vector: rl.Vector2) rl.Vector2 {
 | 
				
			||||||
@@ -139,11 +141,11 @@ fn adjustForGridSystem(input_vector: rl.Vector2) rl.Vector2 {
 | 
				
			|||||||
    return new_vector;
 | 
					    return new_vector;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn calculateArea(val: f32) !void {
 | 
					fn displayAreaSize(val: f32) !void {
 | 
				
			||||||
    var buf: u8[20] = undefined;
 | 
					    var buf: [20]u8 = undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const txt = try std.fmt.bufPrintZ(&buf, "{}", .{val});
 | 
					    const txt = try std.fmt.bufPrintZ(&buf, "{}", .{val});
 | 
				
			||||||
    rl.drawText(txt, SCREENWIDTH - 75, 25, 20, rl.Color.red);
 | 
					    rl.drawText(txt, SCREENWIDTH - 150, 25, 20, rl.Color.red);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const SimpsonsError = error{
 | 
					const SimpsonsError = error{
 | 
				
			||||||
@@ -155,17 +157,17 @@ fn simpsonsRule(start: f32, end: f32, n: usize) !f32 {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const h = (end - start) / @as(f32, @floatFromInt(n));
 | 
					    const h = (end - start) / @as(f32, @floatFromInt(n));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var sum: f32 = getY(start) + getY(end);
 | 
					    var sum: f32 = try getY(start) + try getY(end);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var i: usize = 1;
 | 
					    var i: usize = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // we iterate through amount of subintervals
 | 
					    // we iterate through amount of subintervals
 | 
				
			||||||
    while (i < n) : (i += 1) {
 | 
					    while (i < n) : (i += 1) {
 | 
				
			||||||
        // calculate x of the slice we're adding (start + )
 | 
					        // calculate x of the slice we're adding (start + )
 | 
				
			||||||
        const x = start + i * h;
 | 
					        const x = start + @as(f32, @floatFromInt(i)) * h;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // determine weights 4 for odd, 2 for even
 | 
					        // determine weights 4 for odd, 2 for even
 | 
				
			||||||
        sum += if (x % 2 == 0) 2 * getY(x) else 4 * getY(x);
 | 
					        sum += if (@mod(i, 2) == 0) 2 * try getY(x) else 4 * try getY(x);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return (h / 3.0) * sum;
 | 
					    return (h / 3.0) * sum;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user